Tutorial: Creating Struts Application in Eclipse

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 57

Tutorial: Creating Struts application in

Eclipse
 By Viral Patel on December 4, 2008
 Featured, How-To, Struts, Tutorial

      

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. 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.
o struts.jar
o common-logging.jar
o common-beanutils.jar
o common-collections.jar
o common-digester.jar

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.

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.

view source
print?
01 <servlet>
02         <servlet-name>action</servlet-name>
03         <servlet-class>
04             org.apache.struts.action.ActionServlet
05         </servlet-class>
06         <init-param>

07             <param-name>config</param-name>
08             <param-value>/WEB-INF/struts-config.xml</param-value>
09         </init-param>
10         <load-on-startup>1</load-on-startup>
11 </servlet>
12 <servlet-mapping>
13     <servlet-name>action</servlet-name>
14     <url-pattern>*.do</url-pattern>
15 </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.

view source
print?
01 package net.viralpatel.struts.helloworld.form;
02  
03 import javax.servlet.http.HttpServletRequest;
04 import org.apache.struts.action.ActionErrors;
05 import org.apache.struts.action.ActionForm;
06 import org.apache.struts.action.ActionMapping;
07 import org.apache.struts.action.ActionMessage;
08  
09 public class LoginForm extends ActionForm {
10  
11     private String userName;
12     private String password;
13  
14     public ActionErrors validate(ActionMapping mapping,
15             HttpServletRequest request) {
16  
17         ActionErrors actionErrors = new ActionErrors();
18  
19         if(userName == null || userName.trim().equals("")) {
            actionErrors.add("userName", new
20
ActionMessage("error.username"));
21         }
22         try {
23         if(password == null || password.trim().equals("")) {
            actionErrors.add("password", new
24
ActionMessage("error.password"));
25         }
26         }catch(Exception e) {
27             e.printStackTrace();
28         }
29         return actionErrors ;
30     }

31  
32     public String getUserName() {
33         return userName;
34     }
35     public void setUserName(String userName) {
36         this.userName = userName;

37     }
38     public String getPassword() {
39         return password;
40     }
41     public void setPassword(String password) {
42         this.password = password;
43     }
44 }

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.

view source
print?
01 package net.viralpatel.struts.helloworld.action;
02  
03 import javax.servlet.http.HttpServletRequest;
04 import javax.servlet.http.HttpServletResponse;
05  
06 import net.viralpatel.struts.helloworld.form.LoginForm;
07  
08 import org.apache.struts.action.Action;
09 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionForward;
11 import org.apache.struts.action.ActionMapping;
12  
13 public class LoginAction extends Action {
14  
15     public ActionForward execute(ActionMapping mapping, ActionForm form,
16             HttpServletRequest request, HttpServletResponse response)
17             throws Exception {
18  
19         String target = null;
20         LoginForm loginForm = (LoginForm)form;
21  
22         if(loginForm.getUserName().equals("admin")
23                 && loginForm.getPassword().equals("admin123")) {
24             target = "success";
25             request.setAttribute("message", loginForm.getPassword());
26         }

27         else {
28             target = "failure";
29         }
30  
31         return mapping.findForward(target);
32     }
33 }

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.

view source
print?
1 label.username = Login Detail
2 label.password = Password
3 label.welcome = Welcome
4  
5 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

view source
print?
01 <%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
02 <%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
03  
04 <html>
05     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
06
8859-1">
        <title>Login page | Hello World Struts application in
07
Eclipse</title>
08     </head>

09     <body>
10     <h1>Login</h1>
11     <html:form action="login">
12          <bean:message key="label.username" />
13          <html:text property="userName"></html:text>
14          <html:errors property="userName" />

15          <br/>
16          <bean:message key="label.password"/>
17         <html:password property="password"></html:password>
18          <html:errors property="password"/>
19         <html:submit/>
20         <html:reset/>
21     </html:form>
22     </body>
23 </html>

welcome.jsp

view source
print?
01 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02     pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03
"http://www.w3.org/TR/html4/loose.dtd">
04 <html>

05     <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-
06
8859-1">
        <title>Welcome page | Hello World Struts application in
07
Eclipse</title>
08     </head>
09     <body>
10     <%
11         String message = (String)request.getAttribute("message");
12     %>
13         <h1>Welcome <%= message %></h1>
14  
15     </body>
16 </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:

view source
print?
01 <?xml version="1.0" encoding="ISO-8859-1" ?>
02  
03 <!DOCTYPE struts-config PUBLIC
04           "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
05           "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
06  
07 <struts-config>
08     <form-beans>
09         <form-bean name="LoginForm"
10             type="net.viralpatel.struts.helloworld.form.LoginForm" />
11     </form-beans>
12  
13     <global-exceptions>
14     </global-exceptions>
15     <global-forwards></global-forwards>
16  
17     <action-mappings>
        <action path="/login" name="LoginForm" validate="true"
18
input="/index.jsp"
19             type="net.viralpatel.struts.helloworld.action.LoginAction">
20             <forward name="success" path="/welcome.jsp" />
21             <forward name="failure" path="/index.jsp" />
22         </action>
23     </action-mappings>
24  
    <message-resources parameter="resource.MessageResource"></message-
25
resources>
26  
27 </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)

Login Page
Welcome Page

Related: Create Struts 2 Application in Eclipse

See also:
 Spring 3 MVC – Introduction to Spring 3 MVC Framework
 Struts2 Validation Framework Tutorial with Example
 Inner classes in Java, the mystery within.
 RESTful Web Service tutorial: An Introduction for beginners
 Creating & Parsing JSON data with Java Servlet/Struts/JSP
 Tutorial: Creating JavaServer Faces JSF application in Eclipse
 Struts Validation Framework tutorial with example.
 Struts DispatchAction Tutorial with Example in Eclipse.

Get our Articles via Email. Enter your email address.

Send Me Tutorials http://feeds.feedb viralpatel.net en_US


 

              
  

Tags: framework, Java, Struts, Tutorial, webapp


« Unable to access JBoss server from other machines
AJAX Post method example using javascript & jQuery »

111 Comments on “Tutorial: Creating Struts application in


Eclipse”

Aditi wrote on 4 December, 2008, 19:14

Thanks Viral,
The explanation and information is very apt.
Very useful.

Veera wrote on 4 December, 2008, 21:01

Informative post.

Here’s a similar tutorial, but for the latest version of struts 2 – Create Struts 2 – Hello
World Application

Zviki wrote on 5 December, 2008, 2:14

If you’re (still) doing Struts, you should check out Oracle Workshop for WebLogic. It is
the best Struts tool by far, includes many examples and can do much much more than
Eclipse WTP. It is not WebLogic specific. And, it’s free.

Check out my post about it:


http://blog.zvikico.com/2008/08/the-best-jspstrutsjsf-development-tool-is-now-free.html

Viral Patel wrote on 5 December, 2008, 11:45

@Veera : Nice to see your Struts-2 tutorial. You will see more tutorials on similar line
here on viralpatel.net

@Zviki : Thanks for your comment. Will definitely check Oracle Workshop for
WebLogic.

tabrez wrote on 9 December, 2008, 14:42

Nice tutorial, good job :)

@Zviki I suggest having a look at IntelliJ IDEA’s support for Struts 2, in my opinion it is
one of the best. It’s not free, though.

chn wrote on 15 December, 2008, 21:38

hi did anyone try this with WASCE server? iam doing in it and evrything looks fine
except that when i clikc on submit it says that the resource/login is not found..

any ideas pls help

kingshri wrote on 25 December, 2008, 13:54

I am getting this exception while deploying this …. Can anyone help?

HTTP Status 500 –

——————————————————————————–

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling
this request.

exception

org.apache.jasper.JasperException: Cannot find ActionMappings or ActionFormBeans


collection
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java
:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

javax.servlet.ServletException: Cannot find ActionMappings or ActionFormBeans


collection
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.j
ava:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java
:774)
org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

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:107)
org.apache.jsp.index_jsp._jspService(index_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

teena wrote on 30 December, 2008, 17:41

its very useful for beginners


mayank wrote on 7 January, 2009, 18:14

i am getting this exception ..

HTTP Status 500 –

——————————————————————————–

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling
this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page


/index.jsp at line 12

9:
10: Login
11:
12:
13:
14:
15:

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java
:524)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

javax.servlet.ServletException: javax.servlet.jsp.JspException: Missing message for key


“label.username”
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.j
ava:850)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java
:779)
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:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

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:174)
org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0(index_jsp.java:118)
org.apache.jsp.index_jsp._jspService(index_jsp.java:86)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.

many thanks
mayank

Vivek wrote on 19 January, 2009, 11:24

Copy the following content in your struts-config.xml file.

label.username = Login Detail


label.password = Password
label.welcome = Welcome
error.username =Username is not entered.

did u mean that paste the above content in MessageResource.properties file? or struts-
config.xml file?

Viral wrote on 19 January, 2009, 12:05


Thanks Vivek,
I have updated the error in post.
You need to copy this in MessageResource.properties file.

Thanks again.

yogita wrote on 25 January, 2009, 19:37

Hi
I am getting following error please help…
SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans
collection
at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:712)
at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:500)
at org.apache.jsp.index_jsp._jspx_meth_html_form_0(org.apache.jsp.index_jsp:132)
at org.apache.jsp.index_jsp._jspx_meth_html_html_0(org.apache.jsp.index_jsp:108)
at org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.
java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173
)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213
)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(
Http11Protocol.java:744)
at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerTh
read.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)

Jwalant wrote on 1 February, 2009, 12:27

Hi,

I am getting Error 404–Not Found From RFC 2068 Hypertext Transfer Protocol —
HTTP/1.1:
10.4.5 404 Not Found . I have double checked web.xml and struts-config.xml. I would
appreicate, if anyone can help me.

gribo wrote on 5 February, 2009, 18:17

You should have take the time to make this tutorial with the latest version of struts 1 :
v1.3.10
The version 1.2, on which is based this tutorial, has been released for more than 4 years !
The branch 1.3 which split struts.jar into multiple modules has been release in two years !

Sample wrote on 11 February, 2009, 4:58

Excellent Material for the Beginners.


Follow the steps carefully, it should definitely work.

Thanks
Sample


FrEE wrote on 17 February, 2009, 12:54

Hi,

There is anice tutorial on how to use Struts2, a basic example at

http://www.interview-questions-tips-forum.net/index.php/Apache-Struts/Struts2-
example-tutorial/My-First-example-using-Apache-Struts2

jimish wrote on 20 February, 2009, 16:17

hai,,this ,,gr8,,,,help to intoduce the struts application

Jessiee wrote on 21 February, 2009, 19:17

I m not able to find jar files u mentioned above.Can u provide me the exact path..

Karthic R Raghupathi wrote on 23 February, 2009, 19:43

Greetings!

I found your post very educating. This is the first sample struts application I created using
eclipse.

However, I kept getting the error saying the message was not found in the key bundle.

I tried many things but I got the same error. Finally I modified the entry in the struts-
config.xml to
from what you mentioned. It did the trick.

I’m using struts 1.3, jdk 6 and eclipse ganymede.. hope that helps.


Baran wrote on 2 March, 2009, 14:20

Hello There,

I am fairly new to Struts, I was wondering if anyone can post or preferably mail me some
materila about how to handle database connectivity in Struts. I am working on JNDI but I
guess this won\\\’t be helpful if I have a shared apache tomcat server as that won\\\’t let
me access the server.xml file.

my id is baran.khan @ gmail.com

Payal wrote on 19 March, 2009, 10:49

I am preety new to struts. I want to know how to remove the error Missing message for
key \"label.username\"? I have created a package name resource in src and created a file
in it with the name MessageResource.properties. What changes i need to make in my
struts-config.xml?

Viral Patel wrote on 19 March, 2009, 11:20

Hi Payal,
I am not sure if I got your problem. What I understand is that you want to remove the
error messages generated from keys that are missing in your bundle MessageResource
file. If this is the case then you can use null attribute in <message-resources> tag in
struts-config.xml file.

null attribute will control the messages, If true, references to non existing messages
results in null and If false, references to non existing messages result in warning message
like
???KeyName???
Hope this will solve your query.

Davinchi wrote on 19 March, 2009, 11:31


Hey thanks for this great tut!!

But, I was having the same error “Missing message for key “label.username” and I
moved the file MessageResource to the folder WEB-INF->classes->resource. That did
the trick.

Can you explain why tis happened? Thanks again mate!

Viral Patel wrote on 19 March, 2009, 12:36

While providing the parameter attribute in <message-resource> tag we provide it as


parameter=”resource.MessageResource”. Thus in this case, struts expects the
MessageResource.properties file under resource folder. But if you have provided
parameter=”MessageResource” then that means your MessageResource properties file is
directly under classes folder.

grace wrote on 23 March, 2009, 8:20

Could anyone help me to solve the following error? I have checked the struts-config and
web.xml . But I cannot find the error.please…
,Grace

The server encountered an internal error () that prevented it from fulfilling this request.

javax.servlet.ServletException: Exception creating bean of class


net.viralpatel.struts.helloworld.form.LoginForm: {1}
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.j
ava:846)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java
:779)
org.apache.jsp.index_jsp._jspService(index_jsp.java:91)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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:802)
javax.servlet.jsp.JspException: Exception creating bean of class
net.viralpatel.struts.helloworld.form.LoginForm: {1}
org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:563)
org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:520)
org.apache.jsp.index_jsp._jspx_meth_html_form_0(index_jsp.java:107)
org.apache.jsp.index_jsp._jspService(index_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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:802)

Geek wrote on 25 March, 2009, 12:45

Hi..

To make this tutorial work:

1. also add servlet-api.jar.


2. make sure struts-config.xml is in web-inf folder or the folder u have specified in
web.xml
3. make sure the messageresource file is in web-inf/classes folder.
4. Change the following line in LoginAction.java from
request.setAttribute(\"message\", loginForm.getPassword());
To
request.setAttribute(\"message\", loginForm.getUserName());

Kiran

sachin wrote on 9 April, 2009, 21:25

I have done everything as told here.I am using SDE 3.1 and I am getting this exception
on runing the application.Can anyone help?

SEVERE: Servlet.service() for servlet jsp threw exception


org.apache.jasper.JasperException: The absolute uri: http://jakarta.apache.org/struts/tags-
html cannot be resolved in either web.xml or the jar files deployed with this application
at
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:50)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:114)
at
org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoIm
pl.java:316)
at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:147)
at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:423)
at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1552)
at org.apache.jasper.compiler.Parser.parse(Parser.java:126)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:155)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.
java:252)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173
)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213
)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnec
tion(Http11BaseProtocol.java:664)
at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerTh
read.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)

Sarwo Edi wrote on 7 May, 2009, 11:28

help me,
I am beginner programmer, I got exception in ur tutorial as this

HTTP Status 500 –

——————————————————————————–

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling
this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /index.jsp:13

10: <body>
11: <h1>Login</h1>
12: <html:form action=\"login\">
13: <bean:message key=\"label.username\" />
14: <html:text property=\"userName\"></html:text>
15: <html:errors property=\"userName\"/>
16: <br/>

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java
:451)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause
javax.servlet.ServletException: Missing message for key \"label.username\"
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.j
ava:841)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java
:774)
org.apache.jsp.index_jsp._jspService(index_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

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:166)
org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0(index_jsp.java:110)
org.apache.jsp.index_jsp._jspService(index_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.27 logs.

——————————————————————————–

Apache Tomcat/5.5.27

please help me

Sarwo Edi wrote on 7 May, 2009, 11:40

I already copy
label.username = Login Detail
label.password = Password
label.welcome = Welcome
error.username =Username is not entered.
to the resource folder, but the exception still occured….

please help me

Sarwo Edi wrote on 7 May, 2009, 16:16

hey, thanks very much..


finally I can solve it…

Viral Patel wrote on 7 May, 2009, 21:08

@sarwo edi: nice to see that your problem got solved :) by the way. where was the
problem exactly? I guess your properties file was not getting referred properly?

Sarwo Edi wrote on 11 May, 2009, 12:31

yeah, in struts-config, I only type <message-resources parameter=\"MessageResource\"


null=\"true\"/>, it make its true…

btw, any another eclipse tutorial?


I want to be a good java programmer as you do..

txh

adm wrote on 28 May, 2009, 16:58

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

<message-resources parameter=\"MessageResource\"></message-resources>

noufendar wrote on 29 May, 2009, 14:43

Muchas gracias!!! a Viral por el tutorial y también a Davinchi porque dió la solución al
mismo problema que tenía yo!!!!

Davinchi wrote on 19 March, 2009, 11:31


Hey thanks for this great tut!!

But, I was having the same error “Missing message for key “label.username” and I
moved the file MessageResource to the folder WEB-INF->classes->resource. That did
the trick.

Can you explain why tis happened? Thanks again mate!

Pankaj wrote on 5 June, 2009, 10:40

I have followed ur tutorial

i am getting error

VERE: Servlet.service() for servlet jsp threw exception


javax.servlet.jsp.JspException: Cannot find ActionMappings or ActionFormBeans
collection
at org.apache.struts.taglib.html.FormTag.lookup(FormTag.java:798)
at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:506)
at org.apache.jsp.index_jsp._jspx_meth_html_005fform_005f0(index_jsp.java:115)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:88)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:390)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.
java:290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206
)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228
)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

Last Published: 2008-12-14

Apache | Struts 2 | Struts 1

Struts 1

 Welcome
 Learning
 Roadmap
 Releases

Documentation

 User Guide
 FAQs and HOWTOs
 Release Notes
 Javadoc
 DTDDoc

Support

 User Mailing List


 Issue Tracker (JIRA)
 Wiki Pages

Components

 Struts Apps
 Struts EL
 Struts Extras
 Struts Faces
 Struts Scripting
 Struts Taglib
 Struts Tiles

Project Documentation

 Project Information
 Project Reports

How to setup a basic Struts project using Eclipse IDE


Legal Disclamer

* DISCLAIMER - This simple How-To shows you one of many ways to setup a working project
using
the Struts framework. This is mainly geared toward Struts users who are new to Eclipse, and
don't want to spend a lot of time figuring out the differences between their old IDE (if any)
and this one.

I will also apologize ahead of time for the formatting of this page.

In this How-To, I will demonstrate (using Eclipse 2.0.1) how to setup, compile, run,
and debug the struts-mailreader web application that is part of the Struts Applications subproject.

Next, I will modify the code to pull some data from a MySql database using the popular
relational mapping tool OJB. (This is actually quite simple)
Let's get started
Before we begin, you will need to create a directory somewhere to store your project.
I typically use C:\personal\development\Projects\(some project)
Once that's done, extract the struts-mailreader.war to that directory
(using your favorite zip utility)

Delete the META-INF folder because this will be created during the build/jar/war process.
Add a build.xml file to the project root. I use something like this:

<project name="Struts Mailreader" default="main"


basedir=".">

<!-- This is a basic build script, only the minimums


here -->

<!-- Tell ant to use my environment variables -->


<property environment="env"/>

<property file="./build.properties"/>

<!--
This build script assumes Tomcat 5 is the servlet
container.
Modify as necessary if a different container is being
used.
-->
<property name="tomcat.home"
value="${env.CATALINA_HOME}"/>
<property name="servlet.jar"
value="${tomcat.home}/common/lib/servlet-api.jar"/>
<property name="jsp.jar"
value="${tomcat.home}/common/lib/jsp-api.jar"/>
<property name="deploy.dir"
value="${tomcat.home}/webapps"/>
<property name="build.compiler" value="modern"/>
<property name="build.dir" value="./WEB-INF/classes" />
<property name="src.dir" value="./WEB-INF/src"/>
<property name="war.file" value="struts-mailreader"/>
<property name="war.file.name" value="${war.file}.war"/>

<path id="project.class.path">
<fileset dir="./WEB-INF/lib/">
<include name="**/*.jar"/>
</fileset>
<pathelement path="${src.dir}"/>
<pathelement path="${servlet.jar}"/>
<pathelement path="${jsp.jar}"/>
</path>

<target name="clean">
<delete dir="${build.dir}" includeEmptyDirs="true" />
</target>
<target name="prep">
<mkdir dir="${build.dir}"/>
</target>

<target name="compile">
<javac srcdir="${src.dir}"
destdir="${build.dir}"
debug="on"
deprecation="on">
<include name="**/*.java"/>
<classpath refid="project.class.path"/>
</javac>
</target>

<target name="cleanWebApp">
<delete file="${deploy.dir}/${war.file.name}" />
<delete dir="${deploy.dir}/${war.file}"
includeEmptyDirs="true" />
</target>

<target name="war">
<war warfile="${war.file.name}"
webxml="./WEB-INF/web.xml">
<fileset dir="./" includes="**/*.*" excludes="*.war,
**/*.nbattrs, web.xml, **/WEB-INF/**/*.*,
**/project-files/**/*.*"/>
<webinf dir="./WEB-INF" includes="**/*"
excludes="web.xml, **/*.jar, **/*.class"/>
<lib dir="./WEB-INF/lib"/>
<classes dir="${build.dir}"/>
<classes dir="${src.dir}">
<include name="**/*.properties"/>
</classes>
</war>
</target>

<target name="deploy">
<copy todir="${deploy.dir}">
<fileset dir="./" includes="${war.file.name}"/>
</copy>
</target>

<target name="main" depends="clean, prep, cleanWebApp,


compile, war"/>

</project>
1. Create a new project.
2. New Java Project

3. Browse for the folder you created for your project.


4. Eclipse will detect your source folders from any subdirectories under your project.
5. In our case, this is where the src folder was placed.
6. Default standard libs are automatically added depending on the type of project.
7. Add the existing web app jars.
8. Now we need to add a few jars from the file system.

9. We always need this one (servlet.jar)


10. Ahhhh...everything looks ok for now. You can always go back and modify these settings
later.
11. When everything settles down, you should see something like this (of course, it might look
different depending on your installation/customization):

12. Compilation warnings and errors are detected immediately. In this screenshot, I drill down
into the source folder, package, file, class, and double click on the method....which
brings up the source editor. I hover the mouse over the offending warning to see
a description of what's wrong.
13. I changed ApplicationConfig to ModuleConfig, then saved and now I see new errors.
You can right click and import ModuleConfig right from the error.
14. A quick look at the import section.

15. Right click, Source, Organize Imports


16. Ahhhh...better

17. From the Package Explorer, right click your build.xml and run Ant:
18. Is this cool or what?
19. Uh Oh!

20. Quick look at what jars are being used to process my build.
21. I simply removed all the existing jars from the IDE's Ant configuration and
added all from my own installation.
22. Can't forget that last one

23. Everything went ok (for me)


24. Time to test-drive

© 2000-2008 Apache Software Foundation

Installing Tomcat
This is a brief "how-to" for installing Tomcat on a Windows PC.

Installing Java
Tomcat requires java in order to run. If your computer already has java installed, you can
probably skip this step. However, make sure you have a recent version of java. Here I provide
instructions for installing version 1.4.2 of the Java 2 Platform, Standard Edition (J2SE).

Steps for installing java

1. Go to the download page of J2SE Version 1.4.2.


2. Select the version for Windows and click through the license acceptance. After two pages, you
will be able to download the EXE file for installing java on windows. Look for the SDK version.
3. Download and run the EXE installation program.
4. You will need to accept the license agreement again.
5. Use the suggested directory for installing java (C:\j2sdk1.4.2_01).
6. You may use the remaining default settings for the installation.

Setting the Java Environment Variable


Tomcat will need to know where you have installed java. To do this, you will need to set the
environment variable JAVA_HOME to C:\j2sdk1.4.2_01 (where you installed java).

Here are the steps for setting the environment variable on my computer (Windows XP
Professional). The steps will probably be similar for other Windows computers.

1. Open the control panel under the start menu.


2. Double-click on System.
3. Click on the Advanced tab.
4. Click on the Environment Variables button.
5. Under System Variables, click on the New button.
6. For variable name, type: JAVA_HOME
7. For variable value, type: C:\j2sdk1.4.2_01
8. Continue to click OK to exit the dialog windows.

Installing Tomcat
After setting the JAVA_HOME environment variable, you can install tomcat.

1. Go to the Tomcat Web page.


2. Click on Binaries under the Download label on the left side of the page.
3. Scroll down until you see Tomcat 4.1.x. (x will be some number greater than 10).
4. Click on the link ending with exe (e.g. 4.1.27 exe).
5. Download and run the exe file.
6. I suggest you install Tomcat at c:\tomcat4
7. Use the default settings and provide a password that you will remember.
8. now assume that your tomcat are installed at c:\tomcat4

Running Tomcat
Here are the steps to see if Tomcat has been successfully installed

1. Start Tomcat by finding its start program in the Programs Menu (located in the Start menu).
Look under Apache Tomcat 4.1 and select "Start Tomcat".
2. Open a Web browser and type in the following URL:
o http://localhost:8080/
At this point, you should see the Tomcat home page, which is provided by the Tomcat Web
server running on your computer. Note: if your computer has an internet name or an IP number,
you may access your Tomcat server anywhere on the internet by substituting localhost with the
full name or IP number.

To shut down your server and remove the Console window, select "Stop Tomcat" in the same
menu of where you selected "Stop Tomcat".

. Set Your CLASSPATH


Since servlets and JSP are not part of the Java 2 platform, standard edition, you have to identify the
servlet classes to the compiler. The server already knows about the servlet classes, but the compiler (i.e.,
javac) you use for development probably doesn't. So, if you don't set your CLASSPATH, attempts to
compile servlets, tag libraries, or other classes that use the servlet and JSP APIs will fail with error
messages about unknown classes. Here are the standard Tomcat locations:

Tomcat 4: c:\tomcat4\common\lib\servlet.jar

in addition to the servlet JAR file, you also need to put your development directory in the
CLASSPATH. Although this is not necessary for simple packageless servlets, once you gain
experience you will almost certainly use packages. Compiling a file that is in a package and that
uses another class in the same package requires the CLASSPATH to include the directory that is at
the top of the package hierarchy. In this case, that's the development directory I just discussed.
Forgetting this setting is perhaps the most common mistake made by beginning servlet
programmers!

Finally, you should include "." (the current directory) in the CLASSPATH. Otherwise, you will
only be able to compile packageless classes that are in the top-level development directory.

Here are two representative methods of setting the CLASSPATH. They assume that your
development directory is C:\Servlets+JSP. Replace install_dir with the actual base installation
location of the server. Also, be sure to use the appropriate case for the filenames, and enclose
your pathnames in double quotes if they contain spaces.

 Windows 98/Me. Use the autoexec.bat file.


o Tomcat 4
 Sample code: (Note that this all goes on one line with no spaces--it is broken
here only for readability.)
set CLASSPATH=.; C: \Tomcat4\common\lib\servlet.jar
 Sample file to download and modify: autoexec.bat

Note that these examples represent only one approach for setting the CLASSPATH. Many Java
integrated development environments have a global or project-specific setting that
accomplishes the same result. But these settings are totally IDE-specific and won't be discussed
here. Another alternative is to make a script whereby -classpath ... is automatically
appended onto calls to javac.

 Windows NT/2000/XP. You could use the autoexec.bat file as above, but a more common
approach is to use system settings. On WinXP, go to the Start menu and select Control Panel,
then System, then the Advanced tab, then the Environment Variables button. On Win2K/WinNT,
go to the Start menu and select Settings, then Control Panel, then System, then Environment.
Either way, enter the CLASSPATH value from the previous bullet.

Enable the Invoker Servlet


The invoker servlet lets you run servlets without first making changes to your Web application's
deployment descriptor (i.e., the WEB-INF/web.xml file). Instead, you just drop your servlet into WEB-
INF/classes and use the URL http://host/servlet/ServletName (or
http://host/webAppName/servlet/ServletName once you start using your own Web applications). The
invoker servlet is extremely convenient when you are learning and even when you are doing your initial
development. To enable the invoker servlet, uncomment the following servlet-mapping element in
c:\tomcat4\conf\web.xml. Also, do not confuse this Apache Tomcat-specific web.xml file with the
standard one that goes in the WEB-INF directory of each Web application. Finally, remember to make a
backup copy of the original version of this file before you make the changes.

    <servlet-mapping>
        <servlet-name>invoker</servlet-name>
        <url-pattern>/servlet/*</url-pattern>
    </servlet-mapping>

Create a simple web application


Here are the steps for running the class examples discussed on the first day.

1. Goto the following location on your computer:  C:\Tomcat 4\webapps  


o Create a directory “webdir” under C: \Tomca4\webapps, which will be your home
directory for your assignment.  
o Crate a directory “WEB-INF” under C: \Tomcat4\webapps \webdir
o Crate a directory “classes” under C: \Tomcat4\webapps\webdir\WEB-INF\, which will
hold your servlet class files.
2. Goto http://127.0.0.1:8080/examples/servlets/helloworld.html, copy the java code and paste it
into a blank file in your editor (such as notepate), and save it as

c:\ Tomcat4\webapps\webdir\WEB-INF\classes\HelloWorld.java

3. Open a “Command Prompt” windows, and goto c:\ Tomcat4\webapps\webdir\WEB-


INF\classes\
4. try “javac HelloWorld.java”
5. you will get a HelloWorld.class
6. Stop and restart Tomcat.
7. You can access the first example with the following URL:
o http://localhost:8080/webdir/servlet/HelloWorld
8.  You can work at your own project based on webdir directory, and use some IDEs for
programming, such as Jcreator or EditPlus.

Note: this note is copied partly from http://facweb.cs.depaul.edu/cmiller/ect433/tomcatInstall.html

From someone who want to how to config tomcat, please refer to:

http://www.coreservlets.com/Apache-Tomcat-Tutorial/

You might also like