Tutorial: Creating Struts application in Eclipse

[ad name=”AD_INBETWEEN_POST”] Note: If you looking for tutorial “Create Struts2 Application in EclipseClick here.
In this tutorial we will create a hello world Struts application in Eclipse editor.
We will implement a web application using Struts framework which will have a login screen. Once the user is authenticated, (s)he will be redirected to a welcome page.

Let us start with our first struts based web application.

First let us see what are the tools required to create our hello world Struts application.

  1. JDK 1.5 above (download)
  2. Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
  3. Eclipse 3.2.x above (download)
  4. Apache Struts JAR files:(download). Following are the list of JAR files required for this application.
    • struts.jar
    • common-logging.jar
    • common-beanutils.jar
    • common-collections.jar
    • common-digester.jar

Open Eclipse and goto File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

After selecting Dynamic Web Project, press Next.

Write the name of the project. For example StrutsHelloWorld. Once this is done, select the target runtime environment (e.g. Apache Tomcat v6.0). This is to run the project inside Eclipse environment. After this press Finish.

Once the project is created, you can see its structure in Project Explorer.

Now copy all the required JAR files in WebContent -> WEB-INF -> lib folder. Create this folder if it does not exists.

Next step is to create a servlet entry in web.xml which points to org.apache.struts.action.ActionServlet class of struts framework. Open web.xml file which is there under WEB-INF folder and copy paste following code.


Here we have mapped url *.do with the ActionServlet, hence all the requests from *.do url will be routed to ActionServlet; which will handle the flow of Struts after that.

We will create package strutcures for your project source. Here we will create two packages, one for Action classes (net.viralpatel.struts.helloworld.action) and other for FormĀ  beans(net.viralpatel.struts.helloworld.action).

Also create a class LoginForm in net.viralpatel.struts.helloworld.action with following content.

package net.viralpatel.struts.helloworld.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginForm extends ActionForm {

	private String userName;
	private String password;
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors actionErrors = new ActionErrors();
		if(userName == null || userName.trim().equals("")) {
			actionErrors.add("userName", new ActionMessage("error.username"));
		try {
		if(password == null || password.trim().equals("")) {
			actionErrors.add("password", new ActionMessage("error.password"));
		}catch(Exception e) {
		return actionErrors ;
	public String getUserName() {
		return userName;
	public void setUserName(String userName) {
		this.userName = userName;
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;

LoginForm is a bean class which extends ActionForm class of struts framework. This class will have the string properties like userName and password and their getter and setter methods. This class will act as a bean and will help in carrying values too and fro from JSP to Action class.
Let us create an Action class that will handle the request and will process the authentication. Create a class named LoginAction in net.viralpatel.struts.helloworld.action package. Copy paste following code in LoginAction class.

package net.viralpatel.struts.helloworld.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.viralpatel.struts.helloworld.form.LoginForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String target = null;
		LoginForm loginForm = (LoginForm)form; 
				&& loginForm.getPassword().equals("admin123")) {
			target = "success";
			request.setAttribute("message", loginForm.getPassword());
		else {
			target = "failure";
		return mapping.findForward(target);

In action class, we have a method called execute() which will be called by struts framework when this action will gets called. The parameter passed to this method are ActionMapping, ActionForm, HttpServletRequest and HttpServletResponse. In execute() method we check if username equals admin and password is equal to admin123, user will be redirected to Welcome page. If username and password are not proper, then user will be redirected to login page again.

We will use the internationalization (i18n) support of struts to display text on our pages. Hence we will create a MessagesResources properties file which will contain all our text data. Create a folder resource under src (Right click on src and select New -> Source Folder). Now create a text file called MessageResource.properties under resources folder.

Copy the following content in your Upadate:struts-config.xml MessageResource.properties file.

label.username = Login Detail
label.password = Password
label.welcome = Welcome

error.username =Username is not entered.

Create two JSP files, index.jsp and welcome.jsp with the following content in your WebContent folder.


<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>

		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Login page | Hello World Struts application in Eclipse</title>
	<html:form action="login">
		 <bean:message key="label.username" />
		 <html:text property="userName"></html:text>
		 <html:errors property="userName" />
		 <bean:message key="label.password"/>
		<html:password property="password"></html:password>
		 <html:errors property="password"/>


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Welcome page | Hello World Struts application in Eclipse</title>
		String message = (String)request.getAttribute("message");
		<h1>Welcome <%= message %></h1>

Now create a file called struts-config.xml in WEB-INF folder. Also note that in web.xml file we have passed an argument with name config to ActionServlet class with value /WEB-INF/struts-config.xml.

Following will be the content of struts-config.xml file:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

		<form-bean name="LoginForm"
			type="net.viralpatel.struts.helloworld.form.LoginForm" />


		<action path="/login" name="LoginForm" validate="true" input="/index.jsp" 
			<forward name="success" path="/welcome.jsp" />
			<forward name="failure" path="/index.jsp" />

	<message-resources parameter="resource.MessageResource"></message-resources>

And, that’s it :) .. We are done with our application and now its turn to run it. I hope you have already configured Tomcat in eclipse. If not then:

Open Server view from Windows -> Show View -> Server. Right click in this view and select New -> Server and add your server details.
To run the project, right click on Project name from Project Explorer and select Run as -> Run on Server (Shortcut: Alt+Shift+X, R)

Login Page

Welcome Page

Related: Create Struts 2 Application in Eclipse

Get our Articles via Email. Enter your email address.

You may also like...


  1. Pramod says:

    for fields userName, password null Received in ActionForm class..Any Suggestions?

  2. jinhou says:

    hi,gays,i ‘m a newer in structs ,i have a problem that i can’t find the truct.jar instruct 2.6.3,
    have any suggestions for this problem?

  3. Vasek says:

    Thanks for this great tutorial. I noticed one small discrepancy…
    I assume it should be the loginForm username and not the password assigned as an “message” attribute to the request object in the the ActionForm.

    request.setAttribute(“message”, loginForm.getUserName());

  4. Philip Skogsberg says:

    Should your receive the following error when running the server:

     javax.servlet.jsp.JspException: Missing message for key &quot;label.username&quot; 

    Then you probably need to fix the

     &lt;message-resources parameter=&quot;resource.MessageResource&quot;&gt;&lt;/message-resources&gt; 

    . The server can’t find the MessageResource.properties file because you are pointing to the wrong place. Try removing the “resource”, since struts can find the message resource by itself. Modify it so you have the following:

  5. Priyabrata Dash says:

    I have tried the above example and its working fine. While I started reading struts from books I really got confused and i was about to stop reading. Then i thought that let’s start developing a small application using struts taking help from google. I searched in google and found this website and started implementing the above example now its working fine and also I have got few ideas how it works. Many thanks :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>