0

I am trying to learn servlets using Eclipse Juno. I am trying to run a servlet Hello2.java using Tomcat v7.0. This error of HTTP Status 404 keeps coming up. If there is any error in my code, then How can I debug it. And if there is something by which I can ignore this error please do tell me as soon as possible.

Error Status

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/Hello2")
public class Hello2 implements Servlet {
    private static final long serialVersionUID = 1L;

    ServletConfig config = null;

    public Hello2() {
        super();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config=config;

        System.out.println("Servlet is initialized!");
        System.out.println(serialVersionUID);
    }

    @Override
    public void service(ServletRequest req, ServletResponse res)
            throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter out = res.getWriter();

        out.println("<html><title>");
        out.println("Hello again!");
        out.println("</title><body>");
        out.println("Hello Hello!!!!!!");
        out.println("</body></html>");

        //out.close();
    }

    @Override
    public void destroy() {
        System.out.println("Servlet is destroyed.");
    }

    @Override
    public ServletConfig getServletConfig() {   
        return config;
    }

    @Override
    public String getServletInfo() {
        return "Copyright 2017-2018";
    }

}
Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
  • you are calling the wrong URL . The correct URL goes like : http://localhost:8080/SDM1/Hello2 – Satya Mar 01 '17 at 04:28
  • You don't refer to `.java` file in url. Secondly you don't have to give directory reference. Simply try `http://localhhost:8080/SDM1/Hello2`. – Raman Sahasi Mar 01 '17 at 04:30
  • My by default URL is set like that only. I don't know how to change that. How can I change that. All my other programs are running correctly with the right URLs, but this is not. – Code_Ninja Mar 01 '17 at 08:04

3 Answers3

1

Your URL is incorrect. That's why you get 404 (client error).

Try the URL http://localhhost:8080/SDM1/Hello2

VHS
  • 9,534
  • 3
  • 19
  • 43
0

Those links were correct earlier but then I viewed some posts of other people questioning about the same error error and some solutions were given in which they were trying to add a folder names 'classes' in 'WEB-INF' folder and then adding that folder to the build path. Even if I correct the requesting URL it still gives me this error message.

Code_Ninja
  • 1,729
  • 1
  • 14
  • 38
0

You should call the index file on running/debugging the Project. try this URL http://localhost:8080/ or http://localhost:8080/SDM1/WEB-INF/classes/Hello2.html

sankush
  • 1
  • 4
  • i have tried both but I get the same error every time. – Code_Ninja Mar 03 '17 at 05:14
  • See the URL. It is searching for Hello2 inside WEB-INF/classes.I don't thing this file is available over there. 404 error means it did not get proper path/file/jsp/method – sankush Mar 08 '17 at 10:11