• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Tomcat 404 error

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to run my first servlet using Apache Tomcat 4.1.24.
I've bought a book on J2EE solutions and followed the instructions exactly but I can't get my servlet to run.
This is my servlet code, which I have compiled in C:\Java\tomcat\webapps\myApp\WEB-INF\classes:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestingServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet Testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome to the Servlet Testing Center");
out.println("</BODY>");
out.println("</HTML>");
}
}

My web.xml file which I have put in C:\Java\tomcat\webapps\myApp\WEB-INF is as follows:
<?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-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>
I have also edited the server.xml file (in C:\Java\tomcat\conf) and added the following:
<Context path="/myapp" docBase="webapps/myapp" debug="0" reloadable="true">
</Context>
Now according to the book I'm following (Java for the Web with Servlets, JSP, and EJB: A Developer's Guide to J2EE Solutions - Bundi Kurniawan, New Riders Publishing 2002) I should be able to view the servlet at the following URL:
http://localhost:8080/myapp/servlet/Testing
However I get a 404 error : The requested resource (/myapp/servlet/Testing) is not available.
I have tried moving my servlet to C:\Java\tomcat\webapps\examples\WEB-INF\classes and viewing it at http://localhost:8080/examples/servlet/TestingServlet and it works fine.
I don't really want to do all my development in the examples directory, so I'd like to find a solution as to why it doesn't work in the myapp directory.
Can anybody see the problem? Any suggestions would be much appreciated!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Try cut off the comments regarding
**The mapping for the invoker servlet** in file $TOMCAT_HOME/conf/web.xml
(it has the default parameters for all servlets if you don't have your web.xml for each Context.
Leonildo
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have also edited the server.xml file (in C:\Java\tomcat\conf) and added the following:
<Context path="/myapp" docBase="webapps/myapp" debug="0" reloadable="true">
</Context>


I think the docBase ought to be just myapp and not webapps/myapp.


My web.xml file which I have put in C:\Java\tomcat\webapps\myApp\WEB-INF is as follows:
<?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-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
</web-app>


Add this after you close the servlet tag and before you close the web-app tag.

Make sure you shutdown (if already running) and restart the tomcat server after making the changes.
Saket
 
Niamh Gill
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help guys. I changed my web.xml file in myApp to look like this:
<?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-app>
<servlet>
<servlet-name>TestingServlet</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestingServlet</servlet-name>
<url-pattern>/servlet/Testing</url-pattern>
</servlet-mapping>
</web-app>

This solved the problem!
Thanks again.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic