如何将 .jsp 结尾的 url 模式映射到 servlet?
我想要一个映射一个用于直接指向 JSP 的 URL 到 servlet,但到目前为止我的努力 - 将以下 url 模式映射到我的 servlet...
失败,导致向客户端返回 JSP 处理错误。如何将以 .jsp
结尾的 URL 映射到我的 servlet?
I'd like a map a URL that used to point to a JSP directly onto a servlet, but my efforts so far - mapping the following url pattern to my servlet...
<url-pattern>/folder/myoldjsp.jsp</url-pattern>
have failed, causing a JSP processing error to be returned to the client. How can I map a URL ending .jsp
to my servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的 servlet 没有将请求转发到相关的 JSP,那么它应该可以正常工作。也就是说,这将导致无限调度循环,因为 servlet 本身将在
RequestDispatcher#forward()
调用。如果由于某种原因无法重命名目标 JSP 文件,那么最好的选择是将 servlet 映射到不同的 URL 模式(如
/foo
)并创建一个 filter 映射到/folder/*
(或与请求 URL 最接近的任何内容),这反过来又确定 <一个href="http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getRequestURI%28%29" rel="nofollow noreferrer">HttpServletRequest#getRequestURI()
以/myoldjsp.jsp
结尾,然后将请求转发到 servlet。调用
例如,默认情况下不会对转发的请求 过滤器。如果需要,您可以将 JSP 文件名配置为过滤器
。It should work fine if your servlet is not in turn forwarding the request to the JSP in question. This would namely have resulted in an infinite dispatch loop as the servlet itself would be executed again on the
RequestDispatcher#forward()
call.If renaming the target JSP file is not an option for some reason, then your best bet is to map the servlet on a different URL pattern like
/foo
and create a filter mapped on/folder/*
(or whatever matches the request URL(s) closest) which in turn determines if theHttpServletRequest#getRequestURI()
ends with/myoldjsp.jsp
and then forwards the request to the servlet.E.g.
Filters are namely by default not invoked on forwarded requests. You could if necessary make the JSP file name(s) configureable as filter
<init-param>
.