Basic Struts Login Application
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:
Create the jsp files first what you want to display as a home page and resultant pages.
The action path is /register
create a jsp page to display if the username and password are entered correctly.
#2. success.jsp
If the user enters wrong username and password the resultant page will be forwarded to failure.jsp
#3. failure.jsp
web.xml page is the heart of our application. This page acts as trafiic signal.
#4. web.xml
struts-config.xml is to map our action class and form bean and jsp pages.
#5. struts-config.xml
Form is a bean to get and set the values from jsp pages.
#6. RegisterForm.java
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
The home page will display by default as register.jsp as configured in web.xml page.
you can download the source code and libraries here.
Reference Books:
These are the required files to Develop Struts Login Application:
- register.jsp
- success.jsp
- failure.jsp
- RegisterAction.java
- RegisterForm.java
- struts-html.tld
- web.xml
- struts-config.xml
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.
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.
Other wise it forwards the failure.jsp page.
Reference Books: