Tutorial: Creating Struts application in Eclipse
- By Viral Patel on December 4, 2008
Note: If you looking for tutorial “Create Struts2 Application in Eclipse” Click 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.
- JDK 1.5 above (download)
- Tomcat 5.x above or any other container (Glassfish, JBoss, Websphere, Weblogic etc) (download)
- Eclipse 3.2.x above (download)
- 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.
<servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
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) {
e.printStackTrace();
}
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;
if(loginForm.getUserName().equals("admin")
&& 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.
index.jsp
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login page | Hello World Struts application in Eclipse</title> </head> <body> <h1>Login</h1> <html:form action="login"> <bean:message key="label.username" /> <html:text property="userName"></html:text> <html:errors property="userName" /> <br/> <bean:message key="label.password"/> <html:password property="password"></html:password> <html:errors property="password"/> <html:submit/> <html:reset/> </html:form> </body> </html>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome page | Hello World Struts application in Eclipse</title>
</head>
<body>
<%
String message = (String)request.getAttribute("message");
%>
<h1>Welcome <%= message %></h1>
</body>
</html>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"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="LoginForm"
type="net.viralpatel.struts.helloworld.form.LoginForm" />
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action path="/login" name="LoginForm" validate="true" input="/index.jsp"
type="net.viralpatel.struts.helloworld.action.LoginAction">
<forward name="success" path="/welcome.jsp" />
<forward name="failure" path="/index.jsp" />
</action>
</action-mappings>
<message-resources parameter="resource.MessageResource"></message-resources>
</struts-config>
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)

Welcome Page

Related: Create Struts 2 Application in Eclipse
Get our Articles via Email. Enter your email address.








Very good and running example for beginner.
Thanks
very good and help full tutorial…Thanks
Nice description…easy to learn. thanks a lot.
nicely, keep write tutorial much more heheh .,
Nice Description Very good….Thanks A lot !
Nice description for beginners. Thanks a lot…..
Hi Viral,
I am getting following error.
Can You help me getting out of it?plz..
description The server encountered an internal error () that prevented it from fulfilling this request.
javax.servlet.ServletException: Missing message for key “label.username” in bundle “(default bundle)” for locale en_US
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.login_jsp._jspService(login_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
Hi Sourav.
I had the same error and I solved it as follow.
in the file “strut-config.xml” change this code
for this
I hope this help you to solve your problem too.
greeting
and thank to Viral Patel for the tutorial (Y)
Missing message for key “label.username” in bundle
Plz any one can solve my error.
it’s urgent for one of my project.
hi sourav
to solve ur error in ‘struct-config’ file change ”as” n run program u will get output
I got below error when I run the project :
any one can Please solve my problem ?
Thanks in advace
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Exception creating bean of class net.viralpatel.struts.helloworld.form.LoginForm: {1}
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
Hi Viral,
I am getting following error.
Can You help me getting out of it?plz..
description The server encountered an internal error () that prevented it from fulfilling this request.
javax.servlet.ServletException: Missing message for key “label.username” in bundle “(default bundle)” for locale en_US
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.login_jsp._jspService(login_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
Hii i m getting this error when i am running this code .plz help me as soon as possible
HTTP Status 500 – javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key “label.username”
type Exception report
message javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key “label.username”
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key “label.username”
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key “label.username”
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.index_jsp._jspService(index_jsp.java:110)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
javax.servlet.jsp.JspException: Missing message for key “label.username”
org.apache.struts.taglib.bean.MessageTag.doStartTag(MessageTag.java:233)
org.apache.jsp.index_jsp._jspx_meth_bean_005fmessage_005f0(index_jsp.java:189)
org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0(index_jsp.java:133)
org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.
Apache Tomcat/7.0.37
javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key “label.username”
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.index_jsp._jspService(index_jsp.java:105)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
any one can provide the solution plz this is urgent
Can any one tell me how index.jsp knows which class to call? Based on theory I can see that it should call LoginForm but how will it know?
Hi guys –
Please update your struts-config.xml specifically on this line:
change this to:
This corrected the error. Hope this help!
Great!! it’s very useful for beginners. Thanks a lot
Hi Sourav, Sheelu, Prashant,
Instead of creating MessageResource.properties under resources, create it under the following path
WebContent/WEB-INF/classes
Create a folder “classes” under WEB-INF (Eclipse won’t do this)
And in the struts-config.xml change the following line
to
Regards,
Adarsh
Hi Sourav, Sheelu, Prashant,
Instead of creating MessageResource.properties under resources, create it under the following path
WebContent/WEB-INF/classes
Create a folder “classes” under WEB-INF (Eclipse won’t do this)
And in the struts-config.xml change the following line
to
Regards,
Adarsh
“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).”
The second package should be “net.viralpatel.struts.helloworld.form” not “net.viralpatel.struts.helloworld.action”
Cheers.
Thanks..this on help t me…!
Hi Viral,
Thank you for your posting. I have run this program successfully. I had a few problems initially. I had to download the right jars and the move the message.properties file and everything worked fine. Thank you for helping me write a struts application.
Thanks,
Akira
Hi,
I am getting the below error, kindly help me out to fix this
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 15
12:
13:
14: Login
15:
16:
17:
18:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:791)
org.apache.jsp.index_jsp._jspService(index_jsp.java:106)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans collection
org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:798)
org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0(index_jsp.java:123)
org.apache.jsp.index_jsp._jspService(index_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
i m getting error in this line of index.jsp:
error:
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 11
It works fine for correct username and password. But not working for wrong value or null. Can u pls help me?