• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Mapping problem - please help

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the URI you type in, when you get 404 error?
 
Alexey Gor
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic