INFORMATION

1
2
3
4
5
6
Problem :
Why request which start with “/WEB-INF” cannot be mapped to a servlet/filter ?

Link : 
http://stackoverflow.com/questions/14093446/why-request-which-start-with-web-inf-cannot-be-mapped-to-a-servlet-filter

WEB.XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
						http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

    <filter>
        <filter-name>filter</filter-name>
        <filter-class>demo.DemoFilter</filter-class>
    </filter>
    <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>

    <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>

SERVLET

1
2
3
4
5
6
7
8
9
10
11
12
13
public class DemoServlet extends HttpServlet
{

    @Override
    protected void doGet( HttpServletRequest req, HttpServletResponse resp )
            throws ServletException, IOException
    {
        System.err.println("Servlet invoked");
        throw new RuntimeException("NOT FOUND");
    }

}

FILTER

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class DemoFilter implements Filter
{

    @Override
    public void doFilter( ServletRequest req, ServletResponse res, FilterChain chain )
            throws IOException, ServletException
    {
        System.err.println("filter invoked");
        
        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");
        }
        
        System.err.println("filter invoked end");
    }

    @Override
    public void init( FilterConfig arg0 ) throws ServletException { }

    @Override
    public void destroy() { }

}