Login application Example in Struts2.0

Login application Example in Struts2.0

Previous Home Next

 

This is a second basic examples of how you can make a struts2 login application. Here we are going to make only View using JSP Struts2 Tag Library files. The purpose of this topic is taught you how you can make, run and debug struts2 login application?

 You can download "r4r-Login-program-using-struts2.zip" from our website. To do so just copy and past following URL into address bar of your browser http://r4r.co.in/java/struts/basic/tutorial/r4r-Login-program-using-struts2.zip and then change its extension to .war then import it into your workspace by using any IDEs(eclipse or Netbaens).

Directory Structure of Login Example in Struts 2.0 Using MyEclipse IDE



index.jsp

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

<s:form action="login">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login"/>
</s:form>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts.xml

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="login" class="mypack.LoginAction">
<result name="success">/hello.jsp</result>
<result name="failure">/relogin.jsp</result>
</action>
</package>
</struts>

LoginAction.java

package mypack;
public class LoginAction {
	private String name,password;
	public String execute(){
		if(name.startsWith("a")&&password.startsWith("b"))
			return "success";
		else
			return "failure";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
		}

}

hello.jsp

<%@taglib uri="/struts-tags" prefix="s"%>
<b>Welcome, <s:property value="name"/></b>

relogin.jsp

<b>Invalid UserName and Password !</b>
<jsp:include page="index.jsp"/>

Output




Previous Home Next