Simple web application filter does not filter request

We start our journey with STS and create a new base project "Hello World" Spring MVC. I wanted to add a filter to my application, so I created a filter (HelloWorldFilter.java) with the following doFilter method:

public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("Entering Filter");
        request.setAttribute("hello", "Hello World from HelloWorldFilter!");
        chain.doFilter(request, response);
        System.out.println("Exiting HelloWorldFilter");
    }

According to what I read, it (my filter) should also be defined in the application context since Spring bean (Spring delegates it to my filter - from this manual )

So, in my application context, I have:

<bean id="helloWorldFilter" class="com.yl.mvc.filters.HelloWorldFilter"> </bean>

My web.xml contains the following:

<filter>
    <display-name>HelloWorldFilter</display-name>
    <filter-name>HelloWorldFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>HelloWorldFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

In my .jsp file, I added:

<P><%=request.getAttribute("hello")%></P>

But everything I see on my webpage is null (I was expecting Hello World from HelloWorldFilter!). The filter is not even activated.

Did I miss something?

Thanks in advance, Yogi

+3
1

, .

( spring bean) bean ( ), filter-name ( web.xml).

:

<bean id="helloWorldFilter"...

web.xml:

<filter-name>HelloWorldFilter</filter-name>

, H h - . , bean HelloWorldFilter.

+2

All Articles