- This chapter will introduce a complete web application in Struts 2. The program creates login application without validations.
- The program uses static user name and password. The same program can be modified to have dynamic authentication by using the data stored from database. This will require creation of DOJO classes and JSON objects.
- The program prints username on successful login attempt and error page on failure or error event.
// first.jsp <%-- Document : first Created on : Nov 14, 2014, 12:49:46 PM Author : Infinity --%> <%@page contentType = "text/html" pageEncoding = "UTF-8"%> <%@taglib uri = "/struts-tags" prefix = "s" %> <!DOCTYPE html> <html> <body> <h1> Login Application </h1> <hr/> <s:form action = "verify"> <s:textfield name = "uname" label = "Enter Username" /> <br> <s:password name = "password" label = "Enter Password" /> <br> <s:submit value = "Login" align = "center" /> </s:form> </body> </html>
// Redirect.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package jsp_action; import com.opensymphony.xwork2.ActionSupport; public class Redirect extends ActionSupport{ private static final long serialVersionUID = 1L; private String uname,password; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { if(uname.equals("abc") && password.equals("xyz")) { return SUCCESS; }else return ERROR; } }
// successpage.jsp <%-- Document : next Created on : Nov 14, 2014, 1:19:42 PM Author : Infinity --%> <%@page contentType = "text/html" pageEncoding = "UTF-8"%> <%@taglib prefix = "s" uri = "/struts-tags" %> <!DOCTYPE html> <h1> Welcome <s:property value = "uname" />, you have been successfully logged in.. </h1> <hr/>
//errorpage.jsp <%-- Document : errorpage Created on : Feb 6, 2015, 6:43:24 PM Author : Infinity --% <%@page contentType = "text/html" pageEncoding = "UTF-8"%> <%@ taglib prefix = "s" uri = "/struts-tags" %> <!DOCTYPE html> <html> <body> Login failed...! </body> </html>
// struts.xml <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name = "default" extends = "struts-default"> <action name = "verify" class = "jsp_action.Redirect"> <result name = "success"> successpage.jsp </result> <result name = "error"> errorpage.jsp </result> </action> </package> </struts>
// web.xml <?xml version = "1.0" encoding = "UTF-8"?> <web-app version = "3.1" xmlns = "http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <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> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>jsp/first.jsp</welcome-file> </welcome-file-list> </web-app>
- The application will run as follows:
Figure: First screenshot
Figure: unsuccessful login attempt
Figure: second run of application
Figure: On successful login attempt