JETTY 9.4:如何为同一Pathspec配置多个servlet?

发布于 2025-02-05 21:31:39 字数 1563 浏览 1 评论 0原文

在我使用嵌入式码头的Java应用程序中,我配置了两个Httpservlet,例如Test1和test2 我想根据申请的状态为其中一个服务。

为了模拟状态,我在测试页面上配置了一个带有2个按钮的表单:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out = resp.getWriter();
        out.println("<h1>Test</h1>");
        out.println("<form action=\"\" method=\"post\">\n");
        out.println("<input type=\"submit\" name=\"test1\" value=\"test1\" /><br><br>");
        out.println("<input type=\"submit\" name=\"test2\" value=\"test2\" /><br><br>");
        out.println("</form>");
    }

更改是在帖子中完成的(main.handler是ServletContexThandler配置):

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("test1") != null) {
            Main.handler.addServlet(Test1.class, "/test");
            resp.sendRedirect("/test");
            return;
        }
        if (req.getParameter("test2") != null) {
            Main.handler.addServlet(Test2.class, "/test");
            resp.sendRedirect("/test");
            return;
        }
        resp.sendRedirect("/");
    }

在尝试第一个尝试第一个尝试按钮时,请添加正确的Servlet。并送达。 但是,当返回测试页面并选择另一个按钮甚至相同的按钮时,就会引发异常:

java.lang.IllegalStateException: Multiple servlets map to path /test

如何动态为同一Pathspec提供多个servlets?

In my Java application with embedded Jetty I have two HttpServlets configured, say Test1 and Test2
I want to serve one of them based on the state of the application.

In order to simulate the state I've configured a form with 2 buttons on a test page:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setStatus(HttpServletResponse.SC_OK);
        PrintWriter out = resp.getWriter();
        out.println("<h1>Test</h1>");
        out.println("<form action=\"\" method=\"post\">\n");
        out.println("<input type=\"submit\" name=\"test1\" value=\"test1\" /><br><br>");
        out.println("<input type=\"submit\" name=\"test2\" value=\"test2\" /><br><br>");
        out.println("</form>");
    }

And the change is done in post (Main.handler is the ServletContextHandler configured):

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (req.getParameter("test1") != null) {
            Main.handler.addServlet(Test1.class, "/test");
            resp.sendRedirect("/test");
            return;
        }
        if (req.getParameter("test2") != null) {
            Main.handler.addServlet(Test2.class, "/test");
            resp.sendRedirect("/test");
            return;
        }
        resp.sendRedirect("/");
    }

When trying a button on the first try the right servlet is added and served.
But when returning to the test page and choosing another button or even the same button an Exception is thrown:

java.lang.IllegalStateException: Multiple servlets map to path /test

How can I dynamically serve multiple servlets for the same pathSpec?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2025-02-12 21:31:39

servlet规范不允许这样做。

您正在寻找的是控制器模式,其中您有一个servlet,可以控制基于其他条件执行的任务。

控制器模式是一种超常见的方式,用于大型项目,例如春季,Dropwizard,Struts等。

The Servlet Spec does not allow this.

What you are looking for is the Controller pattern, where you have a single Servlet that controls what tasks to perform based on other criteria.

The Controller pattern is a super common and is used in large projects like Spring, Dropwizard, Struts, etc..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文