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 mapping problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet mapping problem" Watch "servlet mapping problem" New topic
Author

servlet mapping problem

Vince Lee
Greenhorn

Joined: Oct 15, 2006
Posts: 7
hi, I'm new to the forum and I'm wondering if anyone would be able to help me.

I am trying to deploy an application in tomcat 5.5.20.
As part of the deployment descriptor, I have the following:

<?xml...
...
<servlet>
<servlet-name>ABCGenericServlet</servlet-name>
<servlet-class>com.abc.servlet.ABCGenericServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ABCGenericServlet</servlet-name>
<url-pattern>/servlet/ABCGenericServlet</url-pattern>
</servlet-mapping>
...
</web-app>

and under the classes directory, I have com/abc/servlet/ABCGenericServlet.class

when i submit a form with <form action="/servlet/ABCGenericServlet" method="post">, I get the following error:

type Status report

message /servlet/ABCGenericServlet

description The requested resource (/servlet/ABCGenericServlet) is not available.

I've also tried several other uri for form action and url-pattern but can't seem to solve the problem. Any help would be greatly appreciated. Thanks
[ October 15, 2006: Message edited by: Vince Lee ]
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
It should work. Perhaps post your whole web.xml.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
HF Servlets & JSP says the url-pattern should not be the same a real directory, so maybe that is causing a problem. The url-pattern should be a made up, fake name that reveals nothing about your actual directory structure, like "/celebrities/ParisHilton.do".

Also, if you make changes to web.xml, you have to restart tomcat for them to take effect.

Also, I have a book that is a little dated called Murach's Servlets and JSP it uses "servlet" in some of the url's as if "servlet" was a signal to the container to look in the classes directory (when not using servlet mapping). So you could try removing "servlet" from your url, and see if that solves the problem.
[ October 15, 2006: Message edited by: sven studde ]
Vince Lee
Greenhorn

Joined: Oct 15, 2006
Posts: 7
I tried changing the url-pattern (eg. taking out /servlet) and have been getting the same results. I've also tried using another version of Tomcat (5.0.28) and jvm (1.5 -> 1.4.2) and it doesn't seem to help.

my web.xml currently looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>test app</display-name>
<description>
This is the test application
</description>
<servlet>
<servlet-name>ABCGenericServlet</servlet-name>
<servlet-class>com.uwa.servlet.ABCGenericServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ABCGenericServlet</servlet-name>
<url-pattern>/ABCGenericServlet</url-pattern>
</servlet-mapping>
</web-app>

and the form action currently is <form action="/ABCGenericServlet" method="post">

any suggestions would be greatly appreciated. thanks
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
The following web.xml format works for me with tomcat.

1) Try copying and pasting the following over your existing web.xml file:

2) Put your serlvet, which should have the following package statement at the top of the file:

package com.uwa;

in the directory:

.../tomcatDirName/webapps/YourAppName/WEB-INF/classes/com/uwa

3) Call the servlet like this:

<form action="ParisHilton.do" method="post"> [edited]

4) Define a doPost() method in your servlet.
[ October 17, 2006: Message edited by: sven studde ]
Vince Lee
Greenhorn

Joined: Oct 15, 2006
Posts: 7
hmm...still no luck with the modifications. this time i also tried url patterns of /appname/* and form action of "appname/aaa", "/appname/aaa", and "aaa". I am wondering if it's some kind of settings issue, like whether i need any system variables other than JAVA_HOME?

also, would anyone happen to know the class that does servlet mapping? I installed a tomcat plugin for eclipse and hopefully I would be able to debug into why it doesn't get the right page and give me a 404 response. Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Try <url-pattern>/ABCGenericServlet</url-pattern> in web.xml,
and <form action="/appName/ABCGenericServlet" method="post"> in your html file, where appName is the name of your web application (the directory under webapps)


[My Blog]
All roads lead to JavaRanch
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148

hmm...still no luck with the modifications.

Arrgh! Sorry, I made a mistake here:

3) Call the servlet like this:

<form action="/ParisHilton.do" method="post">

The action attribute should be "ParisHilton.do"(without the leading slash). Also, note that the corresponding url-pattern in 1) assumes the form is in the directory:

...webapps/YourAppName

i.e the form is in your app's root directory.

Your form can be in a different directory, but then you have to modify the url-pattern. I did some testing, and I think the way it works is that the request url is created from the action attribute in the form, e.g. "ParisHilton.do", by pre pending that string with the directory that the form is in. So, if your form is in the directory:

.../webapps/YourAppName/forms/myForm.htm

then the request url would be:

.../webapps/YourAppName/forms/ParisHilton.do

The url-pattern starts with a slash:

<url-pattern>/ParisHilton.do</url-pattern>

which signifies the app's root directory, which is:

.../webapps/YourAppName

so the actual url-pattern is:

.../webapps/YourAppName/ + ParisHilton.do

and therefore there wouldn't be a match for the request url. The correct url pattern would be:

<url-pattern>/forms/ParisHilton.do</url-pattern>

because that url pattern is:

.../webapps/YourAppName/ + forms/ParisHilton.do

and that would match the request url.

You have to be careful when forwarding with a RequestDispatcher because if you use the request object to get the RequestDispatcher, and in the RequestDispatcher you list a path that does not have a leading slash, then the path is interpreted as a relative path--and it's relative to the directory of the original requesting url.
[ October 16, 2006: Message edited by: sven studde ]
vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
I found it always useful to append the context path with the mapped url in the jsp. So try this and see if it works


<form action="<%=request.getContextPath()%>/appName/ABCGenericServlet" method="post">

may b this will work.
Vince Lee
Greenhorn

Joined: Oct 15, 2006
Posts: 7
I went and tried something, and i used a url pattern of

<servlet-mapping>
<servlet-name>ABCGenericServlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ABCGenericServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

and a form action of "<%=request.getContextPath()%>/aaa.do", which resolves to "/test/aaa.do". when i submitted the form, it tells me that "The requested resource (/test/jsp/null) is not available".

Does this mean my servlets are somehow not getting translated to jsps while i've actually mapped it correctly?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What are you doing in ABCGenericServlet ? Forwarding or redirecting to another JSP file ?
Vince Lee
Greenhorn

Joined: Oct 15, 2006
Posts: 7
ah, found out what the problem is. my dopost in the servlet was trying to redirect to another jsp, and the jsp wasn't at that location

when i first saw /app/jsp/null i thought it was trying to resolve the form attribute to one of my url patterns because it couldn't find an exact match, and wasn't able to successfully do it.

Thank you everyone for the great help and information. you've all been very patient and the information was very helpful.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148


Originally posted by Vince Lee:
ah, found out what the problem is. my dopost in the servlet was trying to redirect to another jsp, and the jsp wasn't at that location

when i first saw /app/jsp/null i thought it was trying to resolve the form attribute to one of my url patterns because it couldn't find an exact match, and wasn't able to successfully do it.


The same thing happened to me when I was testing the paths. For testing, I was using a servlet that subsequently forwarded to a jsp to display the view. Then I noticed the error message said "jsp". That's why I posted:
You have to be careful when forwarding with a RequestDispatcher because if you use the request object to get the RequestDispatcher, and in the RequestDispatcher you list a path that does not have a leading slash, then the path is interpreted as a relative path--and it's relative to the directory of the original requesting url.

As long as your keyboard and computer screen are still in tact, no worries. This stuff gets really frustrating when you know your code is right, but it won't work.
[ October 17, 2006: Message edited by: sven studde ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet mapping problem
 
Similar Threads
Question about url mapping and context
figuring out servlets?
Need a little more explanation on how to use urlmapper
Problem with Apache Tomcat 5.5.12 and calling Servlets
Tomvat 5.5.11 having trouble finding Servlets