同时使用 Servlet 和 JSP 会导致意外循环

发布于 2024-07-24 13:05:54 字数 1760 浏览 13 评论 0原文

我尝试使用 Servlet 作为控制器层,使用 JSP 作为视图层。 我读过的许多示例/教程都建议这样做:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // add something for the JSP to work on
    request.setAttribute("key", "value");

    // show JSP
    request.getRequestDispatcher("main.jsp")forward(request, response);
}

这对于简单的示例来说效果很好,但是当我加强它时(甚至一点点):

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // add something for the JSP to work on
    request.setAttribute("key", "value");

    String pathInfo = request.getPathInfo();
    if ((pathInfo != null) && (pathInfo.length() > 1)) {
        // get everything after the '/'
        pathInfo = pathInfo.subSequence(1, pathInfo.length()).toString();

        if (pathInfo.equals("example")) {
            request.getRequestDispatcher("alternate.jsp").forward(request, response);
        }
    }

    // show JSP
    request.getRequestDispatcher("main.jsp").forward(request, response);
}

据我所知,如果我去(例如) http://localhost/main/example 它正在访问 servlet,到达它分派到的位置alt.alternate.jsp,然后再次运行该 servlet,但这一次,pathInfo 不等于“example”,而是等于“alternate.jsp”,因此它会进入 main.jsp 调度。

我如何让它运行具有与上面类似的逻辑的不同 JSP 文件?

为了更好地衡量,web.xml 中的映射是:

<servlet>
    <servlet-name>Main</servlet-name>
    <servlet-class>com.example.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Main</servlet-name>
    <url-pattern>/main/*</url-pattern>
</servlet-mapping>

I'm trying to use Servlets as a controller layer and JSPs as a view layer. Many of the examples/tutorials I've read suggest doing somehting like this:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // add something for the JSP to work on
    request.setAttribute("key", "value");

    // show JSP
    request.getRequestDispatcher("main.jsp")forward(request, response);
}

This works fine for the simple example, but when I step it up (even a little):

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // add something for the JSP to work on
    request.setAttribute("key", "value");

    String pathInfo = request.getPathInfo();
    if ((pathInfo != null) && (pathInfo.length() > 1)) {
        // get everything after the '/'
        pathInfo = pathInfo.subSequence(1, pathInfo.length()).toString();

        if (pathInfo.equals("example")) {
            request.getRequestDispatcher("alternate.jsp").forward(request, response);
        }
    }

    // show JSP
    request.getRequestDispatcher("main.jsp").forward(request, response);
}

As far as I can tell what's happening is that if I go to (for example) http://localhost/main/example it's hitting the servlet, getting to where it dispatches to alternate.jsp, then it runs the servlet again but this time instead of the pathInfo equaling "example" it equals "alternate.jsp" so it falls through to the main.jsp dispatch.

How can I have it run different JSP files with some logic similar to above?

Just for good measure the mapping in the web.xml is:

<servlet>
    <servlet-name>Main</servlet-name>
    <servlet-class>com.example.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Main</servlet-name>
    <url-pattern>/main/*</url-pattern>
</servlet-mapping>

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

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

发布评论

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

评论(1

黑寡妇 2024-07-31 13:05:54

奇怪的是,我只是从另一个角度来看这个问题。 请参阅此处,第 7.3 节。 2 Servlet 匹配过程有关匹配顺序的信息。

简短摘要:基于路径的映射胜过基于扩展的映射,因此您劫持了 JSP 映射。

Oddly, I was just looking at this from another angle. See here, section 7.3.2 Servlet Matching Procedure for information on the order of matches.

Short summary: Path based mappings trump extension based mappings, so you're hijacking the JSP mapping.

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