Struts 2 Simple Login Example

Struts 2 Simple Login Example


Posted in : Java Posted on : August 26, 2011 at 7:12 PM Comments : [ 0 ]

In this section you will learn how to develop a simple login application using Struts 2.

Struts 2 Simple Login Example

In this section you will learn how to develop  a simple login application using Struts 2.

In the below example , we incorporate the following things  :

  • JDK1.6
  • Tomcat 6.x
  • Eclipse Galileo 3.5.0
  • Struts 2.0.14

The project hierarchy is given below :

APPLICATION FLOW

First, the login page will appear : (UserName is user & Password is user007)

If login is incorrect following will appear to you :

If login is correct, following will appear to you :

CODE

web.xml   ( /Struts2Login/WebContent/WEB-INF/web.xml )

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts2 Application</display-name>
<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>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>

</web-app>

struts.xml ( /Struts2Login/resources/struts.xml  )

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.devmanuals.LoginAction">
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
</package>
</struts>

ApplicationResources.properties ( /Struts2Login/resources/ApplicationResources.properties )

label.username= UserName
label.password= Password
label.login= Login
error.login= UserName / Password Not Matching. Try again...

Login.jsp ( /Struts2Login/WebContent/Login.jsp )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Devmanuals.com-Hello World Struts Tutorial</title>
</head>

<body>
<h2>Struts 2 Login Example</h2>
<s:actionerror />
<s:form action="login.action" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</s:form>
</body>
</html>

Welcome.jsp ( /Struts2Login/WebContent/Welcome.jsp  )

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Welcome</title>
</head>

<body>
<h2>HELLO !! <font color=blue><s:property value="username" /> </font>!</h2>
<h2><font color=red>WELCOME TO DEVMANUALS</font></h2>
</body>
</html>

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics