[context] tomcat 7-Java 1.7
大家好;我正面临着奇怪的事情。在我的web.xml文件中,我这样映射请求:
web.xml
<web-app>
<filter>
<filter-name>filter</filter-name>
<filter-class>demo.DemoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>demo.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
DemoFilter.java(实现过滤器)
@Override
public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain )
throws IOException, ServletException
{
try
{
chain.doFilter(req, res);
}
catch ( Exception e )
{
System.err.println("error");
((HttpServletResponse) res).setContentType("text/html");
((HttpServletResponse) res).setStatus(HttpServletResponse.SC_NOT_FOUND);
res.getWriter().write("foo");
}
}
DemoServlet.java(扩展HttpServlet)
@Override
protected void doGet( HttpServletRequest req, HttpServletResponse resp )
throws ServletException, IOException
{
System.err.println(req.getRequestURI());
throw new RuntimeException("ERROR");
}
[[1 =预期结果]首先,当我尝试请求“ / foo / bar”时,调用了过滤器和servlet(并且页面显示单词“ foo”)。控制台结果:
/foo/bar
error
[2 =意外的结果]然后,当我尝试请求“ / WEB-INF / foo / bar”时,什么都没有被调用(我的过滤器和servlet没有被击中),并且我得到了Tomcat错误报告:
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.30
EDIT:在这里,我希望得到以下结果:
/WEB-INF/foo/bar
error
我旨在使用我的过滤器来处理异常(包括“ / WEB-INF”调用)。
提前感谢!
PS:我想保留当前的web.xml配置(“ url-pattern” =“ / *”;不使用“ error-page”)
EDIT:我的问题被误解了。我不想在“ WEB-INF”目录下提供文件访问权限。我希望使用我的过滤器和Servlet(url-pattern ='/ *')服务每个请求,即使请求URI以“ / WEB-INF /”开头。谢谢!
/WEB-INF
中的资源根本无法公开访问。问题的表达方式另有说明。这是不对的。 I believe that you just asked about the concrete problem the wrong way。基于评论,我了解到您想在/WEB-INF
中记录访问的文件以及404错误。我
guess实际上是您要记录转发,包含和错误的资源以及直接请求的资源旁边的日志。通常,转发和包含的资源确实隐藏在/WEB-INF
中,以防止直接访问。
默认情况下
,而不是在转发和包含的资源上,也不在错误的资源上(例如在404尝试中)调用”。为此,您需要向映射添加适当的<dispatcher>
条目:<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
这样,过滤器不仅会拦截直接请求的资源,还会拦截转发,包含和出错的资源。(每个都是可选的;
REQUEST
完全不存在时为默认值)
ERROR
调度程序也将使其处理404错误。Web容器将不提供此目录的内容。但是,类可以访问此目录的内容,例如您已在应用程序内的web.xml中映射的servlet。
从这里引用:http://www.servletworld.com/servlet-tutorials/web-application-directory-structure.html
WEB-INF目录是Web应用程序的专用区域,WEB-INF下的任何文件无法通过指定浏览器直接访问目录URL,例如
http://somesite/WEB-INF/someresource.html
。 Web容器将不提供此目录的内容。但是内容应用程序中的类可以访问WEB-INF目录。因此,如果您有任何资源(例如JSP或HTML文档)不想直接从网络浏览器访问,您应该将其放在WEB-INF目录下。
https://download.oracle.com/otn-pub/jcp/servlet-3_1-fr-eval-spec/servlet-3_1-final.pdfhttps://download.oracle.com/otn-pub/jcp/servlet-3.0-fr-eval-oth-JSpec/servlet-3_0-final-spec.pdf