Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使jersey和@webservlet协同工作_Java_Jsp_Networking_Jersey_Servlet Filters - Fatal编程技术网

Java 如何使jersey和@webservlet协同工作

Java 如何使jersey和@webservlet协同工作,java,jsp,networking,jersey,servlet-filters,Java,Jsp,Networking,Jersey,Servlet Filters,如何使jersey和@webservlet协同工作 泽西资源配置: @ApplicationPath("/*") public class ApplicationConfig extends ResourceConfig { public ApplicationConfig() { register(Greetings.class); } } 在resourceConfig中注册的资源: @Path("/login") public class Greeting

如何使jersey和@webservlet协同工作

泽西资源配置:

@ApplicationPath("/*")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}
在resourceConfig中注册的资源:

@Path("/login")
public class Greetings {
    @GET
    public Response getHelloGreeting(@Context HttpServletRequest httpRequest) {
        System.out.println("In the Greetings resource");
        String url= "http://"+httpRequest.getServerName()+":"+httpRequest.getServerPort()+httpRequest.getContextPath();
        String newURL = url+"/login.jsp";
        System.out.println(newURL);
        return Response.seeOther(URI.create(newURL)).build();
    }
}
web servlet

@WebServlet(name = "LoginServlet", urlPatterns = { "/hello" })
public class LoginServlet extends HttpServlet {

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

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            ServletContext servletContext = getServletContext();
            System.out.println("inside login servlet");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
            System.out.println("request forwarded");
        }
    //other functions not important so deleted
}
案例1:访问此

控制台日志:

访问此

案例2:更改应用程序配置资源路径:

@ApplicationPath("/auth")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}
访问此文件时

访问此文件时

疑问:
不知道为什么在第一种情况下login.jsp被阻止:

为什么不显示任何ui。。我想它应该来吗?
为什么不显示任何ui?

使用星号(
*
)无法使用

如果您使用/*,那么您就太贪婪了,并且说它应该始终匹配所有内容,并且永远不会调用默认的servlet

改用
@ApplicationPath(“/”

如果使用/,则替换容器的默认servlet

使用星号(
*
)无法使用

如果您使用/*,那么您就太贪婪了,并且说它应该始终匹配所有内容,并且永远不会调用默认的servlet

改用
@ApplicationPath(“/”

如果使用/,则替换容器的默认servlet


如果您使用的是web.xml,或者将是您的解决方案,但既然您不是,那么可能是您唯一的选择。问题是,当您使用
/*
作为Jersey的servlet映射时,它会占用所有请求。因此对
/hello
的请求将转到Jersey,而不是
LoginServlet
。我链接的这些解决方案所做的是,如果Jersey在Jersey应用程序中找不到请求,则会导致Jersey转发请求。另一种解决方案是将servlet映射更改为类似于
/api/*
(这很常见),然后如果您使用web.xml,您只需在api请求前面加上
/api
,或者将其作为您的解决方案,但由于您不是,因此可能是您唯一的选择。问题是,当您使用
/*
作为Jersey的servlet映射时,它会占用所有请求。因此对
/hello
的请求将转到Jersey,而不是
LoginServlet
。我链接的这些解决方案所做的是,如果Jersey在Jersey应用程序中找不到请求,则会导致Jersey转发请求。另一种解决方案是将servlet映射更改为类似于
/api/*
(这很常见),然后您只需在api请求前面加上
/api

,您能告诉我为什么吗?因为有时候它对我的其他项目有效@akitsme按照链接获取更多详细信息。请求被转发到servlet和资源。。但用户界面仍然没有出现。404error@akitsme你知道为什么吗?因为有时候它对我的其他项目有效@akitsme按照链接获取更多详细信息。请求被转发到servlet和资源。。但用户界面仍然没有出现。404error@akitsme看见
(nothing happens 404 error)
@ApplicationPath("/auth")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}
In the Greetings resource  
http://localhost:8088/ResponseFilterweb/login.jsp             (Ui comes)
inside login servlet                                          (Ui comes)
userid is
Encoded string 
request forwarded