1
votes

I have problem running my servlet. When I try to run it , I get HTTP STATUS 404 the requested resource is not available

I have test.class inside "..Tomcat 7.0\webapps\ROOT\test\WEB-INF\classes"

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

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

        PrintWriter out=response.getWriter();
        java.util.Date today=new java.util.Date();
        out.println("<html>"+"<body>"+"<BR>"+"<B>"+"<center>"+today
                     +"</center>"+"<body>"+"</html>");
    }

}

My web.xml is insinde WEB-INF, and it looks like

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <servlet>
  <servlet-name>test</servlet-name>
  <servlet-class>test</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>test</servlet-name>
  <url-pattern>/test<url-pattern>
  </servlet-mapping>
</web-app>

I've compiled the .java file with:

..ROOT\test\WEB-INF>javac -classpath "C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar" -d classes test.java

2

2 Answers

0
votes

try by giving fully qualified class name in web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <servlet>
  <servlet-name>test</servlet-name>
  <servlet-class>test</servlet-class> <!-- give the fully qualified class name -->
  </servlet>
  <servlet-mapping>
  <servlet-name>test</servlet-name>
  <url-pattern>/test<url-pattern>
  </servlet-mapping>
</web-app>
0
votes

The main problem is your directory structure: Tomcat 7.0\webapps\ROOT\test\WEB-INF\classes. Special directory WEB-INF must be directly under name of application (ROOT in your case).

See Oracle tutorial for more information.

A special directory under the document root, WEB-INF, contains everything related to the application that is not in the public document tree of the application. No file contained in WEB-INF can be served directly to the client. The contents of WEB-INF include:

/WEB-INF/classes/* - The directory for servlet and other classes.

/WEB-INF/web.xml and /WEB-INF/sun-web.xml - XML-based deployment descriptors that specify the web application configuration, including mappings, initialization parameters, and security constraints.