0

I'm trying for a couple of hours now but nothing works need some help here!

I'm trying to run an javaEE webapplication on http://localhost:8080/hello-world

When I go to http://localhost:8080 I see that the server is running. But /hello-world is not found HTTP Status 404 - Not Found.

Project structure:

enter image description here

Web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>MainServlet</servlet-name>
    <servlet-class>controller.MainServlet</servlet-class>
  </servlet>

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

MainServlet:

package controller;

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(urlPatterns="/hello-world")
public class MainServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("TESTTESTTEST");

        request.getRequestDispatcher("/test.jsp").forward(request, response);
    }
}

What am I doing wrong here? Why don't I see the log message: TESTTESTTEST in the console and why is /test.jsp not returned?

Please help me out.

--EDIT

Server configuration:

enter image description here

Jamie
  • 10,302
  • 32
  • 103
  • 186

1 Answers1

1

In web.xml you have configured your MainServlet to match pattern /. Maybe you should use /* url-pattern:

<servlet-mapping>
  <servlet-name>MainServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

See this Q/A for futher reading: Difference between / and /* in servlet mapping url pattern

Please also note that you do not access your application via http://localhost:8080/ if you have deployed it to an arbitrary application name: if the context path is /myWebApp you'll have to invoke http://localhost:8080/myWebApp/ or http://localhost:8080/myWebApp/hello-world respectively.

In case of Apache Tomcat you can name the context path ROOT if you want to access it via http://localhost:8080/ or http://localhost:8080/hello-world.

Edit:

Seemes both mapping specifications are in conflict. Remove the Serlvet definitions from web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

In your git repo you have specified @WebServlet(urlPatterns="/servlet"), in pom.xml you name your artifacId "1" which yields contextpath "/1".

Now invoking http://localhost:8080/1/ goes to index.jsp. Invoking http://localhost:8080/1/servlet is forwarded to test.jsp by your method but results in error 404 as test.jsp does not exist.

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • Thanks for helping. When I add `/*` I get `javax.servlet.ServletException: Exceeded maximum depth for nested request dispatches: 20`. I already add the arbitrary application name so that should be good. – Jamie Feb 10 '19 at 13:24
  • Does it change if you use `/hello-world`? Sounds like you have an infinite loop now MainServlet -> test.jsp -> MainServlet -> test.jsp ... – Selaron Feb 10 '19 at 13:30
  • Then the message is gone... I've put it on Github aswel https://github.com/larsjanssen6/jea6a very weird issue :( – Jamie Feb 10 '19 at 13:40
  • Still not working. The servlet `doGet` is not reached. – Jamie Feb 10 '19 at 13:53
  • when I do `/hello-world` it's going to `index.jsp` instead of the `doGet` method of my `Servlet`. – Jamie Feb 10 '19 at 14:09
  • Have updated my answer as per the findings in your GIT. – Selaron Feb 10 '19 at 14:39
  • Thanks again for your time appreciate it. I've done exactly what you said but still not working. `HTTP Status 404 - Not Found` I've added an image of my server configuration file. Maybe something is wrong here? – Jamie Feb 10 '19 at 15:02
  • And doGet() not invoked? I'm sorry don't know much about Paraya – Selaron Feb 10 '19 at 16:27