Simple Login Application using Struts 2

1 1 1 1 1 1 1 1 1 1 Rating 3.00 (2 Votes)

Requirement

•    Create a Login page to enter user name and password
•    On submit, validate the user name / password against MySQL database
•    If the authentication is successful, forward to home page showing welcome message along with the user name.
•    If the authentication fails, return back to the login page with appropriate error message.
•    If there is exception / errors during authentication process return back to login page with appropriate error message.

 

Pre-requisites

•    MySQL and Tomcat is already installed
•    MySQL is setup with database and tables for use. Refer Simple Login Application for details
•    You have basic knowledge of Java and J2EE web applications.
•    Basic idea about Struts 1 or 2 or at least about MVC design

 

Concepts Covered

•    IDE set up for Struts 2
•    Struts2 key components
•    Difference between struts 1 and struts 2 highlighted

 

 

Environment Setup

  • Download Eclipse for Java EE which is all ready to go with JEE features and install it. Download from Eclipse for JEE Download
  • Create a ‘Dynamic Web Project’ in eclipse with the name Struts2LoginApp.
    Click next, then next and finish.
  • Download latest Struts2 binaries from Struts 2 Binaries Download. At the time of writing this article, struts 2.3.24 was used.
  • Unzip the binaries and copy the following JAR files to the lib directory(<workspace>\Struts2LoginApp\WebContent\WEB-INF\lib) of the newly created project:

commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
commons-logging-1.1.3.jar
commons-logging-api-1.1.jar
freemarker-2.3.22.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.24.jar
xwork-core-2.3.24.jar


Since we make connections to MySQL using JDBC, we would also need to put the JAR for the MySQL JDBC driver (mysql-connector-java-5.1.20-bin.jar). You can download the latest version of the jar from MySQL JDBC Driver Download.


web.xml configuration:
A blank web.xml might have been added to your project under WEB-INF folder. This file is the JSP deployment descriptor. We need to configure the struts 2 controller, which is a servlet filter by the name FilterDispatcher. If you are familiar with Struts1, you will recollect that in the web.xml, we used to make an entry for the ActionServlet which was the controller in struts1.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   
   <display-name>Struts 2 Login App</display-name>
   <welcome-file-list>
      <welcome-file>Login.jsp</welcome-file>
   </welcome-file-list>
   <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>
</web-app>

 


Joomla SEO by MijoSEF