0
votes

I am new to the Servlet Programming, I have configured the Eclipse IDE, and Tomcat Server in my Mac. Then I followed this JavatPoint Tutorial to create a simple Hello World Servlet and run the project as on Tomcat server. But It shows 404.

Then I referred and followed the following answers:

Answer 1: Tomcat Servlet: Error 404 - The requested resource is not available

Answer 2: HTTP Status 404 - on Eclipse with Tomcat

Answer 3: TOMCAT - HTTP Status 404

But nothing worked for me, Web container is configured means when I try this http://localhost:8080 in my browser, It shows Apache Tomcat page.

I am using Eclipse Java EE Development version Mars(4.5.0), Apache Tomcat 8.0.26.

I am trying for couple of days.

Thanks in advance.

Here is my Hello.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Hello
 */
@WebServlet(name="Hello", urlPatterns={"/Hello"})
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<html><body>");
        out.print("<h1>Hello</h1>");
        out.print("</body></html>");
    }
}

My web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyFirst</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

And the Servers view is like: enter image description here

Server Overview:

enter image description here

In this Project name is MyFirst and my servlet name is Hello. After running the project, I am trying with this URL http://localhost:8080/MyFirst/Hello

2
well, fact is, that the request is not dispatched to your servlet. It is impossible to say why without seeing the configuration. Maybe you missed a step in the tutorial?Henry
please post your web.xml contents and the implementation of your servlet!Rami.Q
@Rami.Q - I have posted the servlet file(Hello.java) and web.xml files. If you need any other input to analyze, I can post here.Shanmugaraja G
@Henry - I followed the tutorial, But I didn't change anything in web.xml, I coded only in Hello.java (servlet file).Shanmugaraja G
@ShanmugarajaG : Please check this link as well stackoverflow.com/questions/14392207/…Avinash Reddy

2 Answers

1
votes

Double click on you server instance (in "Servers" view), click on the "Modules" tab underneath, and check the "Path" column. Is it what you expected?

Eclipse always adds your project name into the path by default.

0
votes

try the following:

  1. stop TOMCAT server
  2. right click the server instance in eclipse servers-View then choose properties
  3. under the "General" click the button "switch Location" (after switching is should be "workspace metaData")
  4. click on apply and then OK
  5. restart TOMCAT and try to call your URL localhost:8080/MyFirst/Hello