i am trying to make use of apache tomcat 7. tomcat7 to gain access to java servlets.

I've produced a folder to carry my webapp files underneath the ROOT folder in webapps. The file structure is much like this.

  • Tomcat 7./webapps/myWebApp
    • HelloHome.html
    • WEB-INF
      • web.xml
      • classes
        • com
          • training
            • HelloServlet.java
            • HelloServlet.class

to call the servlet this web.xml code was adopted:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app version="3.0"

   <!-- To save as "hello\WEB-INF\web.xml" -->


   <!-- Note: All <servlet> elements MUST be grouped together and
         placed IN FRONT of the <servlet-mapping> elements -->



the particular java code (put together to develop a class file underneath the classes folder):

//To save as "<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.java"
package com.training;

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

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

      // Set the response MIME type of the response message
      // Allocate a output writer to write the response message into the network socket
      PrintWriter out = response.getWriter();

      // Write the response message, in an HTML page
      try {
         out.println("<head><title>Hello, World</title></head>");
         out.println("<h1>Hello, world!</h1>");  // says Hello
         // Echo client's request information
         out.println("<p>Request URI: " + request.getRequestURI() + "</p>");
         out.println("<p>Protocol: " + request.getProtocol() + "</p>");
         out.println("<p>PathInfo: " + request.getPathInfo() + "</p>");
         out.println("<p>Remote Address: " + request.getRemoteAddr() + "</p>");
         // Generate a random number upon each request
         out.println("<p>A Random Number: <strong>" + Math.random() + "</strong></p>");
      } finally {
         out.close();  // Always close the output writer

then i hit: http://localhost:8080/myWebApp/sayhello
and obtain a 404 error. any ideas in regards to what i'm doing wrong?

myWebApp should live alongside ROOT, not within it.

Tomcat 7 Deployment

Place your myWebApp.war file under webapps folder rather than ROOT, restart Tomcat after which try exactly the same URL.

Try to place your servlet inside a package, and register it in web.xml accordingly. I recall I have experienced this problem with older version tomcat a few years ago.

ROOT is going to be getting tomcat demo programs, place your myWebApp folder in $tomcat_home/webapps (not inside ROOT)

restart tomcat &lifier try exactly the same URL