Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

yet another 404 status code message

 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying out servlet example in Tomcat 5.0.27

I created a simple TestServlet servlet. Compiled and pasted the class file in <tomcathome>\webapps\ROOT\WEB-INF\classes\TestServlet.

Then I modifed the web.xml Below you can find the part of web.xml

<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>

<servlet>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<servlet-class>org.apache.jsp.index_jsp</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>


When I tried to open the servlet
http://localhost:18080/test

I am getting 404 Status error

however if I replace url-patter of TestServlet as
<url-pattern>index.jsp</url-patter>

and try like http://localhost:18080/

I can see the TestServlet's output.

Please let me know what configuration I need to make to resolve the issue with calling the servlet directly
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Put your servlet in a package and ensure that the web.xml gives the package in <servlet-class> and that the location under WEB-INF\classes is correct.
ALL Java classes used in servlets or JSP should be in packages. The reason being that when the class loader sees a class name without a package, it looks in the "current" directory - which you have no control over.
Bill
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did couple of changes as you suggested

1. created folder "mypackage" under classes
2. modifed the java code to include the package construct
package mypackage;

3. compiled the source code (under WEB-INF\classes\mypackage)
4. Modified the web.xml as below

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-apps>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>mypackage.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-apps>

5. Restarted the tomcat server

6. tried to access "http://localhost:18080/test/TestServlet
I am getting same http 404 status message

* "test" is the web application context

7. when I try to access JSP page I can see the results. It's just the servlet I am facing the problem.

Please help!
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I move the sample servlet to ROOT context I see the output.

I guess there is something on setting the context in tomcat 5.x

I don't see the test.xml context file in conf\Catalina\localhost\ folder

Please let me know how to get the context of the web application to be generated...
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<web-apps>
should be
<web-app>

That probably prevents your context from starting successfully, which probably is also why you don't find your context xml file, and certainly why you get a 404.

Also, you're changing the web.xml file under your app's WEB-INF/lib, and not the one in tomcat's conf directory, right?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It's really silly mistake.. I overlooked that.. now everything works fine..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic