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:html> 
 <html:form action = "/register">
  <table border = "0" align = "center">
   <tr>
    <th>UserName</th>
    <td><html:text property = "username"/></td>
   </tr>
   <tr>
    <th>PassWord </th>
    <td><html:password property = "password"/></td>
   </tr>
   
   <tr>
    <td colspan = "2" align = "center">
     <html:submit value = "Register"/></td>
   </tr>
  </table>
 </html:form>
</html:html>

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" %>
 
<html>
 <body>
  <center>
   <font size = "5" color="green">Login Successful</font>
  </center>
 </body>
</html>

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" %>

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

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"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
  <!-- Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
     <param-name>application</param-name>
     <param-value>ApplicationResources</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- The Usual Welcome File List -->
  <welcome-file-list>
    <welcome-file>register.jsp</welcome-file>
  </welcome-file-list>


  <!-- Struts Tag Library Descriptors -->
  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
</web-app>

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"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <data-sources/>
 <form-beans>
  <form-bean name="registerForm" type="com.javabynataraj.RegisterForm"/>
 </form-beans>
 <global-exceptions/>
 <global-forwards/>
 <action-mappings>
  <action name="registerForm" path="/register" type="com.javabynataraj.RegisterAction">
   <forward name="ok" path="/success.jsp"/>
   <forward name="fail" path="/failure.jsp"/>
  </action>
 </action-mappings>
 <controller/>
</struts-config>

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();
  rf.setUsername(user);
  if(user.equals("sathya") && pass.equals("java"))
   return mapping.findForward("ok");
  else
   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.