Share on Google+Share on Google+

How To Develop Login Form In Struts

Apache Struts is an open-source framework for developing J2EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture.

How To Develop Login Form In Struts


Apache Struts  is an open-source framework for developing J2EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt an MVC architecture. It was originally created by Craig McClanahan and donated to the Apache Foundation in May, 2000.

This article  will explain how to develop login form in struts. Struts adopts an MVC  architecture.

Model Part of Login form Example:
Model is basically data of you application. A collection of Java beans and other helper classes that you use for  your application
In Login Form Example ,the following classes will be used--

  1. Action class
  2. ActionForm class 

Action Class:
The action class  is the link between the Struts framework and your business application logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic.

In this example our Action class is  this Action class only forwards the login.jsp. Our Action class returns the ActionForward  called "loginAction", which is defined in the struts-config.xml file . Here is code of our Action Class:

package roseindia.web.struts.action;
* @Author Sushil Pal
* @Web
* @Email [email protected] 
* @Copyright  
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

LoginForm loginform = (LoginForm) form;
if (loginform.getUsername().equalsIgnoreCase("username") && 
   loginform.getPassword().equals("password")) {
// we are in
   return mapping.findForward("success");
} else {
// not allowed
   return mapping.findForward("failure");

Action Class process the specified HTTP request, and create the corresponding HTTP response and Return an ActionForward instance describing where and how control should be forwarded. In our login action class if username field value will be 'username' and password will be 'password' then the mapping will forward to success.jsp otherwise to failure.jsp.

ActionForm Class:
An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side. It must have setters and getters for all the form fields.The ActionForm class is pre-populated with form data when the form is submitted.
In this example our ActionForm class is Here is code of our ActionForm Class:

package roseindia.web.struts.action;

* @Author Sushil Pal
* @Web
* @Email [email protected] 
* @Copyright  
 * Form bean for the Login Entry Screen.

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;

public class LoginForm extends ActionForm
  private String action="add";
  private String username=null; 
  private String password=null;
   private String usertype=null;

public void setAction(String action){

public String getAction(){
   return this.action;

public void setUsername(String username){

public String getUsername(){
  return this.username;

public void setPassword(String password){

public String getPassword(){
   return this.password;

public void setUsertype(String usertype){

public String getUsertype(){
   return this.usertype;

public void reset(ActionMapping mapping,
HttpServletRequest request){



View Part of Login form Example:
The View portion of a web application is most often constructed using JavaServer Pages (JSP) technology. JSP pages can contain static HTML (or XML),plus the ability to insert dynamic content based on the interpretation of special action tags.
In this example our JSP pages are login.jsp, success.jsp, failure.jsp. Here is code of our JSP pages:

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

<html:html locale="true">
<title><bean:message key="welcome.title"/></title>

<script language="javascript">
function validate(objForm){

alert("Please enter UserID!");
return false;

alert("Please enter Password!");
return false;

if(objForm.usertype.selectedIndex == 0 ){

alert("Please Select User Type!");
return false;

return true;


<body bgcolor="white">

<html:form action="/login" method="post"
 onsubmit="return validate(this);"><center>

<table width="400" border="1" align="center"
cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellspacing="2"
cellpadding="1" width="100%" >
<tr bgcolor="#eaeac8">
<td align="left" colspan="2"><font size="5">
User Login</font></td></tr> 

<tr><td colspan="2"><p>&nbsp;</p></td></tr>

<tr align="center">
<td align="right">User ID:</td> 
<td><html:text property="username" size="30"

<tr align="center">
<td align="right">Password:</td> 
<td><html:password property="password" size="30"

<tr align="center">
<td align="right">Login As:</td> 
<html:select property="usertype">
<option>---------------Login As---------------</option>
<option value="Buyer">Buyer</option>
<option value="Seller">Seller</option>
<option value="Guest">Guest</option>

<tr><td colspan="2"><p>&nbsp;</p></td></tr>

<td align="center" colspan="2"><html:submit>
Login Now !</html:submit></td></tr></table>




<[email protected] import="*"%>
<[email protected] import="java.sql.*"%>
<[email protected] import="javax.sql.*"%>
<[email protected] import="java.util.*"%>
<[email protected] import="javax.naming.*"%>
<[email protected] import="javax.servlet.*"%>

//getting value
String username=request.getParameter("username");
String password=request.getParameter("password");
String usertype=request.getParameter("usertype");

<p> Hi , <%=username%><p>
<p align="center"><font size="3" >You Are Successfully
Loged in as <font color="red"><%=usertype%></font>


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

<html:html locale="true">
<title><bean:message key="welcome.title"/></title>

<body bgcolor="white">

<p align="center"><font size="3" color="red">
Sorry your UserId/Password is incurrect</font></p>
<center><p><html:link page="/pages/login.jsp"><font size="3"
 color="blue">Retry click here!</font></html:link></p></center>

Controller Part of Login form Example:
The Controller part is focused on receiving requests from a  web browser, deciding what business logic function is to be performed, and then delegating responsibility for producing the next phase of the user interface to an appropriate View component. The main component of the Controller in the framework is a servlet of class ActionServlet. This servlet is configured by defining a set of ActionMappings.Defining the mapping for our example. Put the following code in struts-config.xml.
Put the following codes within the <form-beans> --------- </form-beans> section.

Put the following codes within the <action-mappings> --------- </action-mappings> section.

  <forward name="success" path="/pages/success.jsp"/>
  <forward name="failure" path="/pages/failure.jsp"/>

How to run this Example:

Installing Struts Application:

Download latest version of Struts from the site of Struts Copy struts-blank.war to tomcat webapps directory. Start tomcat, Tomcat automatically extracts the file and loads the application. Download the code from here and then integrate it with the struts-blank application. Make necessary changes in the struts-config.xml, and compile application using ant. The restart the tomcat. Now your login application is ready. Now open Internet Explorer and type http://localhost:8080/struts-blank/ and press Enter. You browser window should look like:

Now click on the link  Login Page Example the following window will open...
Now type User Id as 'username' and password as 'password' and select a login option then press login button. The success page will display.

Hi , username

   You Are Successfully Loged in as Buyer

 If you enter incorrect user id and password the failure page will display.

   Sorry your UserId/Password is incorrect

    Retry click here!

In this tutorial you have learned how to develop simple login application with struts.



Posted on: January 8, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: How To Develop Login Form In Struts   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 5, 2011

wat abt web.xml file....
January 26, 2012

awesome code