我如何映射“根” Servlet使其他脚本仍然可运行? [英] How can I map a "root" Servlet so that other scripts are still runnable?
问题描述
我尝试构建一个调用类似以下JSP页面的Servlet:
public void doGet(HttpServletRequest req,HttpServletResponse响应)
抛出IOException,ServletException {
req.getRequestDispatcher(/ WEB-INF / main.jsp)。forward(req,resp);
}
我需要这个Servlet响应域的根(例如: http://example.com/ ),所以我在web.xml中使用了以下映射:
< servlet-mapping>
< servlet-name> MainServlet< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>
我遇到的问题是这个匹配所有东西,所以当调度器转发到/ WEB -INF / main.jsp,这与url-pattern匹配,所以Servlet再次运行。这会导致一个循环运行,直到它死于 java.lang.StackOverflowError 。
如何在不阻止其他脚本运行的情况下匹配根目录?
< servlet-mapping> ;
< servlet-name> MainServlet< / servlet-name>
< url-pattern>< / url-pattern>
< / servlet-mapping>
servlet 3.0规范澄清了这一点:
< blockquote>
空字符串()是一个特殊的URL模式,它准确地映射到
应用程序的上下文根目录。
所以它至少应该在3.0容器上工作,并且我已经验证它可以在Jetty 8上运行。
I'm trying to build a Servlet that calls a JSP page similar to the following:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
req.getRequestDispatcher("/WEB-INF/main.jsp").forward(req, resp);
}
I need this Servlet to respond to the domain's root (eg: http://example.com/) so I'm using the following mapping in the web.xml:
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
The problem I'm having is that this matches EVERYTHING, so when the dispatcher forwards to "/WEB-INF/main.jsp" this matches the url-pattern so the Servlet gets run again. This results in a loop that runs until it dies with a java.lang.StackOverflowError.
How can I match the root without preventing other scripts from being runnable?
Use an empty pattern, e.g.
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
The servlet 3.0 spec has clarified this:
The empty string ("") is a special URL pattern that exactly maps to the application's context root
So it should at least work on a 3.0 container, and I've verified that it works on Jetty 8
这篇关于我如何映射“根” Servlet使其他脚本仍然可运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!