tl;dr: 当 servlet(servlet)与通配符 (/a/*
) 绑定(bind)时,从 servlet 调度到绝对路径失败,但当显式绑定(bind) (/b/b
)
我有一个带有 servlet 的 Guice 网络应用程序,该 servlet 将流量分派(dispatch)到存储在 WEB-INF 中的 HTML 文件:
@Singleton
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/hello.html").forward(request, response);
}
}
它配置了一个GuiceServletContextListener
...
public class MyApp extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new MyModule());
}
private static final class MyModule extends ServletModule {
@Override
protected void configureServlets() {
serve("/a/*").with(MyServlet.class);
serve("/b/b").with(MyServlet.class);
}
}
}
...和一个web.xml
:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>MyApp</display-name>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>test.MyApp</listener-class>
</listener>
</web-app>
我在 Tomcat 中启动它并转到 localhost/b/b
,我看到了我的 hello.html
页面。但是,当我转到 localhost/a/a
时,我得到一个 HTTP Status 404 -/WEB-INF/hello.html
。这对我来说似乎很奇怪,因为这两条路径,无论是否通配符,都绑定(bind)到同一个 servlet,而且 HTML 路径是绝对的,并不真正需要解释。
这是错误的结果,是我没有考虑到的某些(对我来说未知的)行为,还是我的配置错误?
编辑:
根据一些进一步的实验,include
似乎按预期工作,这对我来说表明 forward
有一些我错过的额外行为。使用 include
可能会让我暂时停下来,但我仍然想知道我做错了什么。
最佳答案
我找到了一个 GitHub issue这似乎与我遇到的问题相符,这意味着它是 Guice 本身的错误。
该错误最初是在 2010 年报告的,贡献者似乎对此兴趣不大,而负责人却没有任何兴趣,因此它可能永远不会被修复。
为了将来引用,我将尝试暂时使用 include
并考虑在将来的某个时候切换到另一个框架。
关于java - 从带有通配符路径的 servlet 发送时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30433489/