同时使用 Servlet 和 JSP 会导致意外循环
我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奇怪的是,我只是从另一个角度来看这个问题。 请参阅此处,第 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.