Struts 2 Login Example

Struts 2 Login Example

In this example you will see how to create a simple Login Application in Struts 2. We will cover all the important points at different stages

In this tutorial we will see how to create a simple Struts 2 Hello World Application. The following files are needed to create a Hello World Application. By creating we will be able to understand the flow of the struts 2 framework. We are using the Eclipse IDE for the Project which is the most preferable. 

Here are the following directory structure.

  • web.xml

 

  <?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>HelloWorld</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

  • struts.xml

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts

Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

<package name="default" extends="struts-default">

<action name="RegisterAction" class="r4r.RegisterAction">

<result name="SUCCESS">/welcome.jsp</result>

<result name="FAILURE">/index.jsp</result>

<result name="input">index.jsp</result>

</action>

</package>

</struts>

 

  • RegisterAction.java

 

package r4r;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

private static final long serialVersionUID = 1L;

private String userId;

private String pass;

private String msg;

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

public String getPass() {

return pass;

}

public void setPass(String pass) {

this.pass = pass;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

@Override

public String execute(){

if(pass.equals("amar"))

{

setMsg("login success");

return "SUCCESS";

}

else

{

setMsg("login failed...!");

return "FAILURE";

}

}

}

 

  • index.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Login</title>

</head>

<body>

<center>

<div style="width: 600px; height: 500px; background-color: lightgreen; border: 2px solid gray;">

<center>

<s:form action="RegisterAction">

<s:textfield name="userId" label="User-Id:" required="true"/>

<s:password name="pass" label="Password:" required="true"/>

<s:submit value="S I G N - I N"/>

</s:form>

<s:property value="msg"/>

</center>

</div>

</center>

</body>

</html>

 

  • welcome.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>welcome</title>

</head>

<body>

<s:property value="userId"/>

<s:property value="msg"/>

</body>

</html>

 

Download source code:-

Download