File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet not called  - Error in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Reply locked New topic
Author

Servlet not called - Error in web.xml

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
I have a doubt in web.xml. Since i am new to servlets,i request you to tell whether there is anything wrong in the file. I have a html file(index.html) calling a servlet(AddEmployeeDetail). When i run the jboss server, i get the html file. But, on the click of the submit button, the servlet is not called. Can you please clarify it .web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>Arbitron1</display-name>
<servlet>
<servlet-name>AddEmployeeDetail</servlet-name>
<display-name>AddEmployeeDetail</display-name>
<servlet-class>AddEmployeeDetail</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEmployeeDetail</servlet-name>
<url-pattern>/AddEmployeeDetail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/html/index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

Please tell whether is anything wrong in the way, the servlet is called?
Can you please point out the error.


Padma priya N.G.
Be the change you want to be - Mahatma Gandhi
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can you also show your form action ? (The url you use with the submit button). Chances are that you forgot to add the application context path.
For example "action=/AddEmployeeDetail" instead of "action=/myapp/AddEmployeeDetail"


[My Blog]
All roads lead to JavaRanch
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Hello,
Satou kurinosuke
I do not agree with you. The code shown by you is wrong.
you should not use "action=/AddEmployeeDetail". No.
It is wrong.

You have to use like this...>

<form action="/AddEmployeeDetail">
........
.......
</form>
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
In my html file, I have put
<form name="tstest" method="post" action="/AddEmployeeDetail">
Is that wrong.
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Hello Priya.

I have tested with your web.xml file code on my Tomcat server.
It is perfectly ran on my machine.

I have used like this...

your .html file...

xxxxx.html
----------
<html>
<head> <title> ........</title> </head>
<body>
<form action="AddEmployeeDetail">
<input type="text" name="nameField" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>

I have saved this file directly in the Context path. see below..

yourWebApplication-name --------> this is the Context path.
|
-------- xxxxx.html

I have copied my servlet class file in .classes folder.
Let us say the .class file name is

AddEmployeeDetail.class


Now I have opened the browser and typed the address as shown below..

http://localhost:8000/yourWebApplication-name/xxxxx.html

Now xxxxx.html file is displayed.
I have clicked on submit button.
ultimately the corresponding 'AddEmployeeDetail' servlet is executed.

So there is nothig wrong in you code.
I have tested this on my Tomcat Server. with the code provided in your
web.xml file.

-Chaitanya.D
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
I forgot to mention one more thing. I have put the servlet(AddEmployeeDetail) inside /web-inf/classes. I find that when the submit button is clicked, the class mentioned in the form's action element is not called. What could be the reason.
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Hello
Satou kurinosuke
by mistake I have typed action="/AddEmployeeDetail"
/ (slash) should not be used.

This is the write code ->

<form action="AddEmployeeDetail">
........
.......
</form>
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Priya!

Your code shown like this..

<form name="tstest" method="post" action="/AddEmployeeDetail">

you remove / (slash) from action attribute.
use like this...

<form name="tstest" method="post" action="AddEmployeeDetail">

Observe the above like of code - there is no / (slash).

try it. it works fine.

-Chaitanya.D
chaitanya_d1006@yahoo.com
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Is it working perfectly?
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Is it working perfectly?

-Chaitanya.D
chaitanya_d1006@yahoo.com

Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
i am trying with a sample servlet example
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
i am trying with a sample servlet example
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Hi Priya!
Let me give you the sample servlet I have used..

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class AddEmployeeDetail extends HttpServlet
{
public void service(HttpServletRequest request,HttpServletResponse
response)
{
try
{
PrintWriter out=response.getWriter();
out.println ("Servlet called");
}
catch(Exception e)
{ e.printStackTrace();}
}
}

Try with this sample code.
Name it as
AddEmployeeDetail.java
compile it, and copy the file to .classes folder of your web-application.

-Chaitanya.D
chaitanya_d1006@yahoo.com
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Chaitanya Devavarapu:
Hello,
Satou kurinosuke
I do not agree with you. The code shown by you is wrong.
you should not use "action=/AddEmployeeDetail". No.
It is wrong.

You have to use like this...>

<form action="/AddEmployeeDetail">
........
.......
</form>


Satou's code was not wrong.
It is correct.

If you are going to start the form action with a slash, you must include the servletContext. Doing so makes your links or form actions relative to the root of the application and allows you to move your forms in HTML pages, JSPs, or servlets anywhere in your directory structure and still have them work.

I would add, if you are going to put the contextPath in the URL, you should read it from the request instead of hard coding it.

Example:


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by padmapriyagururajan priya:
Hi,
In my html file, I have put
<form name="tstest" method="post" action="/AddEmployeeDetail">
Is that wrong.


Follow Satou's advice and add the context path to the beginning of the url in your action attribute.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by padmapriyagururajan priya:
Hi,
I forgot to mention one more thing. I have put the servlet(AddEmployeeDetail) inside /web-inf/classes. I find that when the submit button is clicked, the class mentioned in the form's action element is not called. What could be the reason.


If your directory is really named "web-inf" you're going to have problems.
It must be "WEB-INF" (case sensitive). If not, please take the time to type these things correctly when posting to this forum. Careless mistakes like this waste the time of those who are trying to help you.
See:
http://faq.javaranch.com/view?AvoidRedHerrings
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
The directory is named as WEB-INF and not web-inf. I am sorry for the same.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Although i changed the method in the form element to </AddEmployeeDetail>, the servlet class is not invoked.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
My class is UserIO just replace UserIO with your class name.. don't give .class extension. and don't get confused with name for UserIO stuff... just change that UserIO with your class name

<servlet-name>UserIO</servlet-name>
<display-name>Name for UserIO</display-name>
<description><![CDATA[Description for UserIO]]></description>
<servlet-class>com.test.UserIO</servlet-class>


Jigar Naik


Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
Now, my form element is
<form name="tstest" method="post" action="/AddEmployeeDetail">

Still, the servlet is not invoked
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
Where is your servlet's .java file. give that path.
e.g
src/com/test/MyServlet.java
here src is folder and com and test are packages and MyServlet.java is a servlet file.

what you need to do is servlet name preceded by all the package name.

<form name="tstest" method="post" action="/com/test/MyServlet">

****************************************************************************
<form name="tstest" method="post" action="/AddEmployeeDetail">
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by padmapriyagururajan priya:
Hi,
Now, my form element is
<form name="tstest" method="post" action="/AddEmployeeDetail">

Still, the servlet is not invoked


Again, this is not correct.
If your URL starts with a forward slash "/" you must include the contextPath.
Either remove the forward slash (which will make your link relative to the page in which it's located) or be sure to include the context path.

If a working example would help, I have several on my site.
They're all war files so you should only need to drop them into your webapps directory for them to run.
http://simple.souther.us.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Servlet class is directly in classes folder of web-inf. Hence,in the form element, i have directly used /AddEmployeeDetail
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
I am sorry. It is WEB-INF and not web-inf. I have put the class in classes folder and not in any other way.
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
i suggest you to go thru Head First Servlet and JSP.

your consepts will be clear.

and do take care of upper and lower letters case.

All the best
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 753
i dont have idia about how tomcat works. i use JBoss & JBoss Eclipse IDE.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by padmapriyagururajan priya:
Servlet class is directly in classes folder of web-inf. Hence,in the form element, i have directly used /AddEmployeeDetail


URLs are not relative to the WEB-INF directory.
If you add a forward slash to your relative URL, your context path will be skipped.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Ben, thanks for the support

Chaitanya,
Saying that you do not agree is ok. Howeverm, saying that it's wrong without even trying to explain why, is not really nice, don't you think ?

padmapriyagururajan,
did you finally make it work ? If not, please post your form action, and your servlet mapping.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
The html file does not call the servlet even now. The form element is <form name="tstest" method="post" action="/AddEmployeeDetail">
and the servlet class is the same as what Chaitanya has given(AddEmployeeDetail) and web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>Arbitron1</display-name>
<servlet>
<servlet-name>AddEmployeeDetail</servlet-name>
<display-name>AddEmployeeDetail</display-name>
<servlet-class>AddEmployeeDetail</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEmployeeDetail</servlet-name>
<url-pattern>/AddEmployeeDetail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/html/index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5


It appears that you have not put your servlet into a package. The JVM looks for classes not in a package in the "current" directory - which you have no control over. ALL classes involved in servlets and JSP should be in packages correctly declared in web.xml and reflected in the class location under WEB-INF/classes.

Bill
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Priya!
Can you give the complete directory structure of your web-application?
including your static and dynamic resources.
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Priya!
Along with the complete directory structure of your web-application,
please give the code of .html form and web.xml file once again.

Note :
no need to give the complete code of .html form, just mention the code
of <form> tags.
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
The form element in the html form is
<form name="tstest" method="post" action="/AddEmployeeDetail">


web.xml is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<display-name>Arbitron1</display-name>
<servlet>
<servlet-name>AddEmployeeDetail</servlet-name>
<display-name>AddEmployeeDetail</display-name>
<servlet-class>com.AddEmployeeDetail</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddEmployeeDetail</servlet-name>
<url-pattern>/AddEmployeeDetail</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/html/index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>


The directory structure is

Arbitron.war - application name
| |
html(folder) web-inf
| |
index.html classes
|
com
|
AddEmployeeDetail(servlet class)


This is the directory structure.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by padmapriyagururajan priya:
Hi,
The form element in the html form is
<form name="tstest" method="post" action="/AddEmployeeDetail">


Unless your app is the root app in your webserver, the forward slash in front of your relative URL is going to prevent the browser from making a successful request to your servlet.

Several people have told you this and you don't seem to want to fix this.
What is the URL of the page that has the form in it?
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
Priya!
It seems that you kept the
AddEmployeeDetail.class file under the folder --> com.
But,
I am recommending you to put the above servlet file directly inside
the classes folder as shown below. But not in any other sub-directory.

classes
|
-------- AddEmployeeDetail.class


After that, please do the following modification to your code -

Your code is like this :
<form name="tstest" method="post" action="/AddEmployeeDetail">

But what I am saying is, remove the ---> / (forward slash)
Use like this :

<form name="tstest" method="post" action="AddEmployeeDetail">

Observe the above line of code carefully - there is no
/ (forward slash) before AddEmployeeDetail. Is it clear?

Once again saying, please remove --- / (forward slash).
again and again I am saying, remove --- / (forward slash).

I have already told this along before. but it seems that you are not
trying it. Ok, no problem at least please do it now.

Try this.
and Please let me know whether it is executing properly or not.

Chaitanya.D
chaitanya_d1006@yahoo.com
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

You would solve your problems by taking what people say into consideration.

Chaitanya, I don't know why you insist on removing the slash. Are you going to tell us why you want to remove it. Please refer to what Ben said before answering
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
Hi,
I tried to give the below form element.
<form method="post" name="tstest" action="AddEmployeeDetail"
I also put the class directly in class folder and removed it from com folder. Still, i was not able to get the output. The html page was
called, but the servlet class was not called.

I got the below message
"Context not found" for the servlet class. I was not able to resolve
it. Please clear the doubt.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

padmapriyagururajan priya,

You have been given your answer and yet you continue to ignore it. Read the replies from Ben and Satou. Anyone who tells you that the leading slash is not required is dead-out wrong and should be ignored.

You can take the advice of the experienced people who are trying to help you, or you can continue to have problems. The choice is yours.

bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Padma priya Gururajan
Ranch Hand

Joined: Oct 05, 2006
Posts: 434
As per the directory structure i have mentioned, i have the following
url:
http://localhost:8080/Arbitron/html/index.html
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

This message for padmapriyagururajan priya and Chaitanya Devavarapu,

Use can use above pencil pad(icon) to Edit your post when you want to change something.

Thanks,


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet not called - Error in web.xml
 
Similar Threads
How to set up 2 web applicaion in an ear file?
servlet deployment error in web.xml on WSAD 4.x
Listener started twice
What's wrong with my deployment descriptor?
problem in web.xml