2
votes

I'm new to servlets. I've installed Tomcat7 in Ubuntu14.04 and the service runs fine. But it gives 404(not found) error for servlet (http://localhost:8080/hello/HelloServlet). The html within the application runs fine. So are the Tomcat managerial tools.

Any idea of what I'm doing wrong?

Tomcat webapps structure: /var/lib/tomcat7/webapps

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     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_2_5.xsd"
     version="2.5">
<servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>HelloServlet</servlet-name>
     <url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

HelloServlet.java:

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

@WebServlet(name="HelloServlet", urlPatterns={"/HelloServlet"})

public class HelloServlet extends HttpServlet{
  private String message;
  public void init() throws ServletException{
      message = "Hello World";
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  public void destroy(){

  }
}
3
which url you are trying to access? - Garry
Did you tried: http://<server>/hello/HelloServlet ? - Lexandro
localhost:8080/hello/HelloServlet gives the 404 error. Sorry I missed this essential part in the question. - sariDon

3 Answers

1
votes

Finally I got it running :)

It was the directory structuring that was creating the problem. The application directory should be just outside the 'webapps/ROOT' directory. To be specific, just put the application directory directly under the 'webapps' (Seems to be specific case for Tomcat7 and Ubuntu 14.04LTS)

The corrected structure:

webapps
|-- ROOT
`-- hello
    |-- index.html
    `-- WEB-INF
        |-- classes
        |   |-- HelloServlet.class
        |   `-- HelloServlet.java
        |-- lib
        `-- web.xml

The previous structure (with error):

webapps
|-- ROOT
    |-- hello
    |   |-- index.html
    |   `-- WEB-INF
    |       |-- classes
    |       |   |-- HelloServlet.class
    |       |   `-- HelloServlet.java
    |       |-- lib
    |       `-- web.xml
    |-- index.html
    `-- META-INF
        `-- context.xml
0
votes

The java source file should not be in the /WEB-INF folder. Servlet is a regular java file and should be located in the /src/... folder. also, once you annotate the class as @WebServlet - u can remove the web.xml file ( although it has many other usage, using it for 'servlet-mapping' can be ignored for simplification).

├───build
│   └───classes
│       └───pkg
│               HelloServlet.class
│
├───src
│   └───pkg
│           HelloServlet.java
│
└───WebContent
    ├───META-INF
    │       MANIFEST.MF
    │
    └───WEB-INF
        └───lib
0
votes

Your java class file location wrong, change your java class file to src folder.