当前位置: 首页 > 知识库问答 >
问题:

如何让泽西和@webservlet一起工作

芮叶秋
2023-03-14

如何让jersey和@webservlet协同工作?

泽西资源配置:

@ApplicationPath("/*")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}

在resourceConfig中注册的jersey资源:

@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:访问此http://localhost:8088/ResponseFilterweb/login

控制台日志:

In the Greetings resource  
http://localhost:8088/ResponseFilterweb/login.jsp             (no ui comes)

访问这个http://localhost:8088/ResponseFilterweb/hello

(nothing happens 404 error)

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

@ApplicationPath("/auth")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(Greetings.class);
    }
}

访问此
http://localhost:8088/ResponseFilterweb/auth/login

In the Greetings resource  
http://localhost:8088/ResponseFilterweb/login.jsp             (Ui comes)

访问此
http://localhost:8088/ResponseFilterweb/hello

inside login servlet                                          (Ui comes)
userid is
Encoded string 
request forwarded

疑问:
不知道为什么要登录。在第一种情况下,jsp被阻止:

为什么?http://localhost:8088/ResponseFilterweb/login没有显示任何用户界面。。我觉得应该来吗<为什么http://localhost:8088/ResponseFilterweb/hello没有显示任何用户界面?

共有2个答案

仉运乾
2023-03-14

使用星号(*)在使用@ApplicationPath时不起作用

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

改用@ApplicationPath(“/”

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

慕容灿
2023-03-14

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

 类似资料:
  • 我试图在一个我的组件中使用Tesseract来执行文件上的ocr。 .ts: .html 我遵循了这个,但是这个错误显示了 我应该怎么做才能让这个工作成功?

  • 我只是很难让我的控制器单元测试正常工作,因为在我看来,如果使用OAuth,SpringDoc中的内容是不够的。在我的例子中,是Oauth2和JWT。 我尝试使用,,甚至使用和自定义定义我自己的注释,但在计算安全表达式时,总是在UserSecurityContext中获得匿名用户,无论我在工厂中设置测试上下文的是什么。。。 我提出了我刚刚想到的解决方案,但由于我不确定嘲笑令牌服务是最有效/干净的方法

  • 我有一个项目,我正在通过GSON和Volley阅读一些json。我想把我的数据保存在数据库中,我希望Realm是一个好的解决方案。我选择了我的第一个类,它有七个成员变量,所有的Strings和int,并让它扩展RealmObject,并将其中一个int确定为主键。它编译得很好,但是当它运行时,我在日志中得到大量的输出,最终应用程序在显示主要活动之前就崩溃了。GSON似乎不喜欢解析扩展了RealmO

  • 我花了太多的时间(我想超过10个小时)试图弄清楚如何让基本的json调用(来自angularjs)在我的泽西2.4上打开和处理。我已经在谷歌上尝试了所有可能的结果,但仍然在得到一个 415(不支持的媒体类型) 客户端和 组织。玻璃鱼。运动衫消息内部的MessageBodyProviderNotFoundException:找不到媒体类型=应用程序/json,类型=类的MessageBodyRead

  • 错误: 找不到媒体type=application/xml、type=class com.example.DemoRest2.Employee、genericType=class com.example.DemoRest2.Empayee的MessageBodyWriter。 我想返回Employee的对象。类,但获取上述错误。我正在使用@XmlRootElement注释。然而,当我以字符串格式返

  • 我已经成功地建立了TailwindCSS上Gridsome以下说明:https://gridsome.org/docs/assets-css/#tailwind 但是,这些说明并没有提到如何设置autoprefixer。因此,我自己尝试了一下,如下所示: npm安装自动刷新器 修改了文件(请参见下面的修改代码,其中包含我所更改内容的注释) 运行 将类添加到以查看是否添加了任何供应商前缀 结果。。。