views:

48

answers:

1

A web.xml of ours contains following excerpt..

<servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

<filter>
    <filter-name>anotherServlet</filter-name>
    <filter-class>com.test.anotherServlet</filter-class>    
</filter>
<filter-mapping>
    <filter-name>anotherServlet</filter-name>
    <url-pattern>*.htm</url-pattern>
</filter-mapping>

I need to understand as to how the container maps when an *.htm (say hello.htm ) url is encountered ..what happens when such a request happens.

+1  A: 

The second mapping is for a filter, not a servlet.

When a request comes in the servlet container, it is first passed through a chain of any filters, then to the servlet, then back out through the filters in reverse order.

Filters have a slightly different API from servlets: There's a method called doFilter() that gets a ServletRequest and a ServletResponse. It calls the rest of the chain via chain.doFilter with the same parameters; at the end of the filter chain, those parameters are passed to the servlet. So filters are able to change or even substitute the request object coming in, and the response object coming out.

There's a little more info here.

Carl Smotricz
Carl, Thanks for quick response.. i went through that link.. so if there are multiple filter mappings .. <filter-mapping> <filter-name>firstFilter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> <filter-mapping> <filter-name>secondFilter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping>then, first , the doFilter() method of firstFilter is called, and in that if the chain.doFilter() is called, the doFilter of secondFilter is called and the finally the service method of firstServlet is called .. Is the understanding correct ?
JWhiz
Yep, that's correct.
Carl Smotricz
Cool... thanks... that helped to understand..
JWhiz