RequestDispatcher转发被重新路由回到同一个Servlet(RequestDispatcher forwarding is getting rerouted back to the sa

编程入门 行业动态 更新时间:2024-10-23 07:23:57
RequestDispatcher转发被重新路由回到同一个Servlet(RequestDispatcher forwarding is getting rerouted back to the same Servlet)

我有一个名为User.java的servlet。 它映射到url模式

<servlet-mapping> <servlet-name>User</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>

在Servlet内部,分析了user/斜杠后面的路径,从数据库中检索有关该用户的数据,在属性中设置,然后显示页面user_home.jsp 。 实现这一目标的代码是:

User user = UserManager.getUserInfoById(userPath); request.getSession().setAttribute("user", user); request.getRequestDispatcher("resources/jsp/user_home.jsp").forward(request, response);

问题是,不是打开这个user_home.jsp ,而是将请求再次映射到同一个servlet User.java 。 它什么都不做。

我把输出语句放在doGet方法的开头,所以我可以看到URL是

http://localhost:8080/myproj/user/resources/jsp/user_home.jsp

所以看起来很明显的问题是它正在映射回user/*模式。

如何在不经过URL映射的情况下让Servlet显示此页面,并正确显示我需要的jsp ?

I have a servlet called User.java. It is mapped to the url pattern

<servlet-mapping> <servlet-name>User</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping>

Inside the Servlet, the path following the slash in user/ is analyzed, data about that user is retrieved from the database, set in attributes, and then the page user_home.jsp is to be displayed. The code to make this happen is:

User user = UserManager.getUserInfoById(userPath); request.getSession().setAttribute("user", user); request.getRequestDispatcher("resources/jsp/user_home.jsp").forward(request, response);

The problem is, that rather than opening this user_home.jsp, the request is mapped once again to the same servlet User.java. It does nothing.

I've put output statements at the beginning of the doGet method, so I can see that the URL is

http://localhost:8080/myproj/user/resources/jsp/user_home.jsp

so it seems the obvious problem is that it's mapping right back to the user/* pattern.

How do I get the Servlet to display this page without going through URL mapping, and properly display the jsp I need it to?

最满意答案

如果传递给request.getRequestDispatcher()的路径不以“ / ”开头,则将其解释为相对于当前路径。 由于您的servlet的路径是/user/<something> ,它会尝试将请求转发到/user/resources/jsp/user_home.jsp ,它与您的servlet映射匹配,因此会递归转发到同一个servlet。

另一方面,如果传递给request.getRequestDispatcher()的路径以“ / ”开头,则将其解释为相对于当前上下文根。 因此,假设resources目录位于webapp的根目录,请尝试在路径的开头添加“ / ”,例如:

request.getRequestDispatcher("/resources/jsp/user_home.jsp").forward(request, response);

If the path passed to request.getRequestDispatcher() does not begin with a "/", it is interpreted as relative to the current path. Since your servlet's path is /user/<something>, it tries to forward the request to /user/resources/jsp/user_home.jsp, which matches your servlet mapping and therefore forwards to the same servlet recursively.

On the other hand, if the path passed to request.getRequestDispatcher() begins with a "/", it is interpreted as relative to the current context root. So assuming that the resources directory is located at the root of your webapp, try adding a "/" at the beginning of the path, e.g.:

request.getRequestDispatcher("/resources/jsp/user_home.jsp").forward(request, response);

更多推荐

本文发布于:2023-07-25 22:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   RequestDispatcher   Servlet   rerouted   forwarding

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!