wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet Mapping problem - please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Mapping problem - please help" Watch "Servlet Mapping problem - please help" New topic
Author

Servlet Mapping problem - please help

Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
Hello Ranchers,
I have encountered some issues while playing around with servlet mappings. I am trying to map simple requests to the HelloWorldExample servlet that came with Tomcat. It does not seem to work (404 errors). Both application and error Logs are empty.
my web.xml:

<servlet>
<servlet-name>HI</servlet-name>
<servlet-class>HelloWorldExample</servlet-class>
</servlet>

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

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

<servlet-mapping>
<servlet-name>HI</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>

/servlet/* works. the /*.do and /test/* do not. What am I doing wrong? Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
That HelloWorldExample servlet is a special case - they really should put in a better example, this has caused a lot of trouble for beginners.

/servlet/ works because of the invoker see this FAQ for details.

The web.xml mapping to HI does not work because the HelloWorldExample class is not in a defined package. The JVM tries to look in the "current" directory for classes in the default package - and the current directory is somewhere else. You would have to
1. change the code to use a package and recompile,
2. include the name of the package in the <servlet-class> tag,
3. place the compiled class file under WEB-INF/classes using the package name

Bill
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
Thank you for your response. I have tried it and am still having problems. Here is the modified HelloWorldExample code:

package test;

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

public class HelloWorldExample extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");

out.println("<title>YO</title>");
out.println("</head>");
out.println("<body bgcolor=\"white\">");

out.println("<h1>yO</h1>");
out.println("</body>");
out.println("</html>");
}
}

Here is my web.xml:
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<servlet>
<servlet-name>Config</servlet-name>
<servlet-class>Config</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>HelloWorldExample</servlet-name>
<servlet-class>test.HelloWorldExample</servlet-class>
</servlet>

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

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

</web-app>

I get no errors in any of the logs. I get errors if I put something there that should give an error (like using something other than the name specified in <servlet> in mappings) I have tried placing the HelloWorldEXample servlet into WEB-INF\classes\test\ and straight into WEB-INF\classes\ Error 404 every time. I was able to call /servlet/test.HelloWorldExample okay, so the invoker works and the class is in the package....

Thanks again!
Alex
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

What is the URI you type in, when you get 404 error?
Alexey Gor
Greenhorn

Joined: Jul 13, 2004
Posts: 27
To try and hit this mapping:

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

I use:
http://server_name/anything.do
siva raju
Ranch Hand

Joined: Oct 10, 2003
Posts: 37
Try changing the web.xml mapping to

<url-pattern>*.do</url-pattern>
instead of
<url-pattern>/*.do</url-pattern>

From the specification any string other than strings
starting with '*.'
starting with '/' and ending with '/*'
or the string '/'

should constitute an exact match.

If you want to use the pattern

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

then try changing the uri to

http://server_name/contextpath/*.do (anything.do wont do)


Siva<br />-----<br />SCWCD 1.4, SCJP 1.4, SCBCD(Preparing), CCNA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Mapping problem - please help