Simple Struts 2 Tutorial in eclipse with tomcat 7 server

There are loads and loads of Struts 2 Tutorials on the web but most of them are confusing for beginners and mostly outdated. Here is a very simple, basic and easy to understand tutorial.

Before we start you must make sure that you too are working with Apache Tomcat version 7.0.xx or higher (Apache Tomcat version 9.0 is yet in Beta), make sure you have Eclipse IDE with J2EE plugin and you have downloaded the struts2 related jar files ( and have created a Server under eclipse.

My Configurations and Setups :
  1. Operating system: Mac OS X 10.10.2 (Yosemite)
  2. Application Server : Apache Tomcat version 7.0.61
  3. IDE: Eclipse Indigo Service Release 2 (20120216-1857)
Struts 2 Login Page Example

This is a very simple example to get started, we will have an index.php page with two fields username and password, if username = "code2care" and password ="1234" the LoginAction lets you to Homepage.jsp or else to ErrorLogin.jsp, let's get started!

1. Create a new Project under Eclipse Project Explorer: SimpleStrutsEx

2. Place the index.jsp page under SimpleStrutsEx -> WebContent

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
<title>Simple Struts Example : Code2care</title>
   <h1>Login Page : </h1>
   <form action="loginForm">
      <label for="uname">Enter UserName : </label>
           <input type="text" name="uname"/><br/>
       <label for="password">Enter Password : </label>
         <input type="password" name="password"/><br/>
      <input type="submit" value="Login"/>

3. Create HomePage.jsp file under SimpleStrutsEx -> WebContent

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Welcome : Admin</title>
   Welcome :  <s:property value="uname"/>

4. Create LoginError.jsp page and place it also under : SimpleStrutsEx -> WebContent

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<title>Login Error!</title>
  No user found!! :   <s:property value="uname"/>

5. Now lets create a package under SimpleStrutsEx -> Java Resources -> src and name it com.code2care.struts

6. Under the package, we just created in step 5 place our Action class called
package com.code2care.struts;

public class LoginAction {

	private String uname;
	private String password;

	public String execute() throws Exception {

		if(uname.equals("code2care") && password.equals("1234")) 
			return "SUCCESS";
			return "ERROR";	

	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;


7. Now lets create our web.xml file under : SimpleStrutsEx -> WebContent -> WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	xmlns="" xmlns:web=""
	id="WebApp_ID" version="2.5">

8. Now create a folder named classes under : SimpleStrutsEx -> WebContent -> WEB-INF

9. Create struts.xml under classes folder w created at step 8.

<?xml version="1.0" encoding="UTF-8"?>
   "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<constant name="struts.devMode" value="true" />
   <package name="SimpleStrutsEx" extends="struts-default" >
      <action name="loginForm" 
            class="com.code2care.struts.LoginAction" method="execute">
            <result name="SUCCESS">/HomePage.jsp</result>
              <result name="ERROR">/LoginError.jsp</result>

Note that you have added all Struts supporting jars under SimpleStrutsEx -> WebContent -> lib folder.


Source Code Download Link: Dropbox:

Download PDF: Strut2 Simple Tutorial with Example.pdf

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap