我如何映射“根"?Servlet 以便其他脚本仍然可以运行? [英] How can I map a "root" Servlet so that other scripts are still runnable?
问题描述
我正在尝试构建一个调用类似于以下 JSP 页面的 Servlet:
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);
}
我需要这个 Servlet 来响应域的根(例如:http://example.com/)所以我在 web.xml 中使用以下映射:
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>
我遇到的问题是它匹配所有内容,因此当调度程序转发到/WEB-INF/main.jsp"时,它匹配 url-pattern,因此 Servlet 再次运行.这会导致循环运行,直到它以 java.lang.StackOverflowError.
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?
推荐答案
使用空模式,例如
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
servlet 3.0 规范对此进行了澄清:
The servlet 3.0 spec has clarified this:
空字符串 ("") 是一种特殊的 URL 模式,它精确地映射到应用程序的上下文根
The empty string ("") is a special URL pattern that exactly maps to the application's context root
所以它至少应该在 3.0 容器上工作,我已经验证它在 Jetty 8 上工作
So it should at least work on a 3.0 container, and I've verified that it works on Jetty 8
这篇关于我如何映射“根"?Servlet 以便其他脚本仍然可以运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!