Powered by Blogger.

Basic Struts Login Application

>> Sunday, December 18, 2011

 This is our first Application on Struts for Beginners. Before going to start our application go through the Struts flow and architecture.

These are the required files to Develop Struts Login Application:
  1. register.jsp
  2. success.jsp
  3. failure.jsp
  4. RegisterAction.java
  5. RegisterForm.java
  6. struts-html.tld
  7. web.xml
  8. struts-config.xml
Project Structure:
Basic Struts Login Application Project Structure

Create the jsp files first what you want to display as a home page and resultant pages.
The action path is /register

#1. register.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
 <html:form action = "/register">
  <table border = "0" align = "center">
    <td><html:text property = "username"/></td>
    <th>PassWord </th>
    <td><html:password property = "password"/></td>
    <td colspan = "2" align = "center">
     <html:submit value = "Register"/></td>

create a jsp page to display if the username and password are entered correctly.

#2. success.jsp

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
   <font size = "5" color="green">Login Successful</font>

If the user enters wrong username and password the resultant page will be forwarded to failure.jsp

#3. failure.jsp

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

   <font size = "5" color = "red">Login Failure</font><br>
   <a href = "register.jsp">Try Again</a>

web.xml page is the heart of our application. This page acts as trafiic signal.

#4. web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

  <!-- Action Servlet Configuration -->

  <!-- Action Servlet Mapping -->

  <!-- The Usual Welcome File List -->

  <!-- Struts Tag Library Descriptors -->

struts-config.xml is to map our action class and form bean and jsp pages.

#5. struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
  <form-bean name="registerForm" type="com.javabynataraj.RegisterForm"/>
  <action name="registerForm" path="/register" type="com.javabynataraj.RegisterAction">
   <forward name="ok" path="/success.jsp"/>
   <forward name="fail" path="/failure.jsp"/>

Form is a bean to get and set the values from jsp pages.

#6. RegisterForm.java

package com.javabynataraj;
import org.apache.struts.action.*;

public class RegisterForm extends ActionForm
 private String username = null;
 private String password = null;

 public void setUsername(String username)
  this.username = username;
 public String getUsername()
  return username;
 public void setPassword(String password)
  this.password = password;
 public String getPassword()
  return password;

This is the main business logic to run our application in execute method with parameters mapping,form,request,response. This method returns ActionForward with mapping.findForward("string");

#7. RegisterAction.java

package com.javabynataraj;

import org.apache.struts.action.*;
import com.javabynataraj.RegisterForm;
import javax.servlet.http.*;

public class RegisterAction extends Action
 public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws Exception
  RegisterForm rf = (RegisterForm)form;
  String user = rf.getUsername();
  String pass = rf.getPassword();
  if(user.equals("sathya") && pass.equals("java"))
   return mapping.findForward("ok");
   return mapping.findForward("fail");
Deploy your application in Tomcat Server and run the application.

The home page will display by default as register.jsp as configured in web.xml page.

Basic Struts Login Application_001

Enter the username and password as hard coded in our action class. If it is success then it forwards to success.jsp page to display as a result.

Basic Struts Login Application_002
Other wise it forwards the failure.jsp page.

Basic Struts Login Application_003

 you can download the source code and libraries here.

Download Application

Reference Books:

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.