0

I created just a dynamic web project in Eclipse and when I run the the helloworld webapp then I see the index.html page with no problems screenshot: enter image description here

But when I go to the /HelloServlet path then I'm getting a 404 error screenshot: enter image description here

However when I go to localhost:8080 then I can see that Tomcat is running screenshot: enter image description here

In the logs I can also read that the server is running screenshot: enter image description here

Here are my server properties screenshot: enter image description here

And here is the code of HelloServlet.java:

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public HelloServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @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());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Anyone has a clue how to fix this? At my work I had installed and configured Eclipse with Tomcat with no problems. But somehow I can't manage to get it working at home.

superkytoz
  • 1,267
  • 4
  • 23
  • 43
  • Do you have `web.xml` file? – Roman C Oct 08 '16 at 15:09
  • Why is it set up to launch your installed copy of Tomcat and its set of webapps instead of what is in your workspace? When you expand the entries in the `Servers` View, is your project even there? – nitind Oct 08 '16 at 15:13
  • @RomanC here is my web.xml: http://pastebin.com/2abThY2n But I don't think that's the case, because this project that I had created was generated by Eclipse. Which did work at my job. I just created this HelloWorld project also at my home to see if I can run Java EE projects with Eclipse. – superkytoz Oct 08 '16 at 18:40
  • @nitind I don't know I had tried different solutions that were given on StackOverflow. I thought I just needed it to get it working. Unfortunately it didn't work out. Here is screenshot of the `Servers` view: http://i.imgur.com/fMB85AU.png – superkytoz Oct 08 '16 at 18:41
  • @superkytoz So what do you think is the case? – Roman C Oct 08 '16 at 18:52
  • @nitind I have changed the server location back to `Use workspace metadata`. Here is a screenshot of it: http://i.imgur.com/aTH13K9.png But unfortunately I'm still getting a 404 error. Here is screenshot of it: http://i.imgur.com/XgInUsp.png I really don't know what I'm doing wrong now. Can you or someone else help me, please? – superkytoz Oct 08 '16 at 18:57
  • @RomanC To be honest I don't know as I had mentioned in my comment above of 3 minutes ago. I'm spending hours just to run a HelloWorld Java EE webapplication.. I have now changed the server location back to `Use workspace metadata`. But it still doesn't work. – superkytoz Oct 08 '16 at 19:01

2 Answers2

0

Change the line of code has annotation @WebServlet become to:

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

You will be happy.

To avoid exception (When application context is /), you should check request.getContextPath().

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

@WebServlet(name = "HelloServlet", urlPatterns = "/HelloServlet")
public class HelloServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public HelloServlet() {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getContextPath().length() > 0) {
            response.getWriter().append("Served at: ").append(request.getContextPath());
        } else {
            response.getWriter().append("Served at: " + "/");
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • I don't think it has something to do with the code. Because I had also created this HelloWorld project at my work. Which did work. It's a problem with Eclipse and/or Tomcat I think. I also can't run all of my other Java EE projects.. – superkytoz Oct 08 '16 at 18:44
0

Credits to the accepted answer on this link: HTTP Status 404 - on Eclipse with Tomcat

On the Eclipse Project menu I didn't had Build Automaticallychecked. After that I had checked it. I can invoke the doGet() method of the HelloServlet class.

Community
  • 1
  • 1
superkytoz
  • 1,267
  • 4
  • 23
  • 43