0

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

Community
  • 1
  • 1
Shanmugaraja G
  • 2,778
  • 4
  • 31
  • 47
  • 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 Sep 07 '15 at 17:14
  • 1
    please post your web.xml contents and the implementation of your servlet! – Rami.Q Sep 07 '15 at 17:37
  • @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 Sep 07 '15 at 20:08
  • @Henry - I followed the tutorial, But I didn't change anything in web.xml, I coded only in Hello.java (servlet file). – Shanmugaraja G Sep 07 '15 at 20:18
  • @ShanmugarajaG : Please check this link as well http://stackoverflow.com/questions/14392207/http-error-404-when-running-tomcat-from-eclipse – Avinash Reddy Sep 08 '15 at 06:00
  • @ShanmugarajaG: Here is also another link https://www.youtube.com/watch?v=orctlc_F5Y0 – Avinash Reddy Sep 08 '15 at 06:03

2 Answers2

1

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.

  • Under the server, I can see the Modules, But when I run the project, In my browser, It shows result HTTP 404. – Shanmugaraja G Sep 07 '15 at 18:11
  • 1
    @ShanmugarajaG Yes but what is in "Path" column there? And what is in the address bar of your browser? –  Sep 07 '15 at 18:25
  • I have edited question with the screenshot and in the browser the link is http://localhost:8080/MyFirst/Hello – Shanmugaraja G Sep 07 '15 at 20:15
  • Have a look at this tutorial https://www.youtube.com/watch?v=b42CJ0r-1to&list=PLE0F6C1917A427E96. This explains how to configure tomcat in eclipse. Hope this should help – Avinash Reddy Sep 08 '15 at 05:47
0

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
Rami.Q
  • 2,486
  • 2
  • 19
  • 30