从另一个 servlet 调用 servlet 的 service()

发布于 2025-01-02 02:09:19 字数 1043 浏览 1 评论 0原文

我的 web.xml 中有一个 url 映射,这样对特定 url x.pt 的请求就会映射到 Servlet(例如 Servlet1)。在此 servlet 的 service() 中,我检查请求是否具有某些特定参数。

如果是这样,则通过实例化另一个 servlet Servlet2 并调用其服务方法,将该调用委托给它。

public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    // if the call is for some special events (request has some specific parameter)
    if (req.getParameter(conditionCheck()) {
        doPost(req, res);
    } else {
        // Report parsing
    }
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    try {
        // instantiate Servlet2 object
        servlet2.init(this.getServletConfig());
        servlet2.service(req, res);
        servlet2.destroy();
    } catch (Exception e) {
        LOG.error("Unable to execute event", e);
    }
}

当请求是针对特殊事件(即 Servlet2)时,浏览器返回一些 JSON 文本 如果我需要做一些额外的事情来获取 Servlet2 对浏览器的响应,请告诉我。

提前致谢!

I have a url-mapping in my web.xml such that requests for a specific url x.pt gets mapped to a Servlet say Servlet1. In the service() of this servlet I check if the request has some specific parameter.

If so, the call is delegated to another servlet Servlet2 by instantiating it and calling its service method.

public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    // if the call is for some special events (request has some specific parameter)
    if (req.getParameter(conditionCheck()) {
        doPost(req, res);
    } else {
        // Report parsing
    }
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    try {
        // instantiate Servlet2 object
        servlet2.init(this.getServletConfig());
        servlet2.service(req, res);
        servlet2.destroy();
    } catch (Exception e) {
        LOG.error("Unable to execute event", e);
    }
}

The browser returns some JSON text when the request is for special events( i.e. to Servlet2)
Do let me know if I need to do something extra for getting response of Servlet2 to the brwoser.

Thanks in advance!

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

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

发布评论

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

评论(2

无法言说的痛 2025-01-09 02:09:19

您可以使用 RequestDispacher 转发您的请求:

RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);

You can forward your request using RequestDispacher:

RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
苄①跕圉湢 2025-01-09 02:09:19

正如 Kris 所说,我希望 RequestDispatcher 能够工作,但是当我看到像这样直接调用 servlet 时,我总是感到不舒服。您是否有机会将 servlet2 提供的逻辑移动到 servlet1 和 servlet2 都可以调用的单独对象中?如果可以的话,我认为它会给你一个更好、更容易测试的解决方案。

As Kris says, I'd expect a RequestDispatcher to work, but I'm always uncomfortable when I see a servlet being called directly like this. Do you have the opportunity to move the logic that is provided by servlet2 into a separate object that both servlet1 and servlet2 can call upon? If you can, I think it'll give you a better, more easily testable solution.

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