Simple Login Application using Struts 2

Article Index

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


•    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.



•    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:


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=""
   id="WebApp_ID" version="3.0">
   <display-name>Struts 2 Login App</display-name>



Joomla SEO by MijoSEF