Pages Navigation Menu

Coding is much easier than you think

Login & Logout Example in Struts 2

In this tutorial we will see how to create a simple Struts 2 Login Application. The following files are needed to create a Login Application.
** UPDATE: Struts 2 Complete tutorial now available here.
Final project structure
Login Action Example in struts 2

  1. Create a New Dynamic Web Project in eclipse and give a name to your project
    Action class
    file :

    package com.action;
    public class LoginAction {
    	private String userName;
    	private String password;
    	public String execute() {
    		if (userName.isEmpty() || password.isEmpty())
    			return "error";
    			return "success";
    	public String getUserName() {
    		return userName;
    	public String getPassword() {
    		return password;
    	public void setUserName(String userName) {
    		this.userName = userName;
    	public void setPassword(String password) {
    		this.password = password;

    File : login.jsp use the Struts 2 tags to display username and password input fields and submit button.

    <%@taglib uri="/struts-tags" prefix="s"%>
    	<s:form action="login">
    		<s:textfield name="userName" label="User Name" />
    		  <s:password name="password" label="Password" />
    		<s:submit value="Submit" />


    file : success.jsp :€“ A JSP view page to display a welcome message to user.

    <%@taglib uri="/struts-tags" prefix="s"%>
    <s:head />
    	Welcome <s:property value="userName" /> 
    	, you are logged in successfully

    file :error.jsp :€“ A JSP view page to display error message when a wrong user id and password is entered

    <title>Un Authorised</title>
    	<h1>Login failed</h1>

    Struts configuration file
    The configuration in struts.xml should be as shown below

        <package name="default" extends="struts-default">
    	<action name="login" class="com.action.LoginAction">
    		<result name="success">success.jsp</result>
    		<result name="error">error.jsp</result>

    Make sure you have done the mapping properly in web.xml file as shown below


    Now on running this application in TomCat server, the application will deliver the JSP as below


    Login IN struts 2
    ** Update – The below screen shot is the demo of Tab Style Login and Signup feature using jQuery in Java web application, read the article here to learn about the same
    Tab Style Login Signup using jQueryin Java

    About Mohaideen Jamil

    I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: