为什么以“/ WEB-INF”开头的请求无法映射到servlet / filter [英] Why request which start with “/WEB-INF” cannot be mapped to a servlet/filter

查看:331
本文介绍了为什么以“/ WEB-INF”开头的请求无法映射到servlet / filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[context] tomcat 7 - java 1.7

嘿大家;我面对陌生人的工作。
在我的web.xml文件中,我映射了这样的请求:

Hey everyone; I'm facing with stranges working. In my web.xml file, I mapped request like this :

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)

DemoServlet.java (extends 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')。控制台结果:


[1 = Expected result] First, when I tried to request "/foo/bar", the filter and the servlet were called (and the page display the word 'foo'). Console result :

/foo/bar    
error

[2 =意外结果] 然后,当我尝试请求/ WEB-INF / foo / bar时,没有调用任何内容(我的过滤器和我的servlet没有被击中)我得到一个Tomcat错误报告:

[2 = Unexpected result] Then, when I tried to request "/WEB-INF/foo/bar", nothing was called (my filter and my servlet were not hit) and I get a Tomcat error report :

HTTP Status 404 -

type Status report

message

description The requested resource is not available.
Apache Tomcat/7.0.30

编辑:在这里,我希望得到以下结果:

EDIT : Here, I expected to get the following result :

/WEB-INF/foo/bar    
error






我的目标是使用我的过滤器来处理异常(/ WEB-INF 包括电话)。


I aim to use my Filter to handle Exception ("/WEB-INF" calls included).


  • 为什么请求哪个以/ WEB-INF /开头的内容从未在/ *模式上映射过滤器/ servlet?

  • 我们如何解决此问题?

  • Why request which start with "/WEB-INF/" never hit filter/servlet mapped on "/*" pattern ?
  • How can we solve this problem?

提前致谢!

PS :我想保留当前的web.xml配置(url-pattern =/ *;不使用错误页面)

PS : I would like to keep my current web.xml configuration ("url-pattern" = "/*" ; without using "error-page")

EDIT
我的问题被误解了。我不想在WEB-INF目录下提供文件访问权限。我想用我的Filter和Servlet(url-pattern ='/ *')为每个请求提供服务,即使请求URI以/ WEB-INF /开头。谢谢!

推荐答案

这里存在一个重大的概念误解。 / WEB-INF 中的资源根本不可公开访问。您提出问题的方式另有说明。这个不对。 我相信您刚刚以错误的方式询问具体问题

There's a major conceptual misunderstanding here. Resources in /WEB-INF aren't publicly accessible at all. The way how you put the question indicates otherwise. This is not right. I believe that you just asked about the concrete problem the wrong way.

根据评论,我知道你想在 / WEB-INF 中记录访问过的文件以及404错误。我猜测你实际上意味着你想要在直接请求的资源旁边记录转发,包含和错误的资源。通常,转发和包含的资源确实隐藏在 / WEB-INF 中,以防止直接访问。

Based on a comment, I understood that you wanted to log accessed files in /WEB-INF as well as 404 errors. I guess that you actually meant that you want to log forwarded, included and error'ed resources as well next to directly requested resources. Normally, the forwarded and included resources are indeed hidden in /WEB-INF to prevent direct access.

过滤器默认情况下仅在直接请求的资源上调用,而不是在转发和包含的资源上,也不会在404尝试的错误资源上调用。为此,您需要将相应的< dispatcher> 条目添加到映射中:

A filter is by default invoked on directly requested resources only, not on forwarded and included resources nor on error'ed resources like in your 404 attempt. For that you need to add the appropriate <dispatcher> entries to the mapping:

<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 是完全缺席时的默认值

(each of them are optional; the REQUEST is the default one when totally absent)

这样过滤器不仅会拦截直接请求的资源,还会拦截转发的,包含的和错误的资源。 ERROR 调度程序也会使它启动404错误。

This way the filter will not only intercept on directly requested resources, but also on forwarded, included and error'ed resources. The ERROR dispatcher will make it to kick in on the 404 error as well.

这篇关于为什么以“/ WEB-INF”开头的请求无法映射到servlet / filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆