简体   繁体   中英

JavaEE cannot acces servlet on payara server

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:

在此处输入图片说明

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:

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM