views:

77

answers:

2

Could you help to check why doFilter not getting called

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<filter>
<filter-name>roseFilter</filter-name>
<filter-class>net.paoding.rose.RoseFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>roseFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
</web-app>

class signature:

import org.springframework.web.filter.GenericFilterBean;
public class RoseFilter extends GenericFilterBean {

404 is returned while call http://localhost:8080/hello/world, I set the breakpoints at doFilter, it seems doFilter not called?(I tried tomcat 6.0.18, 6.0.29, jdk1.6)

A: 

What's the web request look like? Can you try changing your url-pattern to *.jsp instead of / * ? If you are using something other than pure JSP then change it to whatever the request ending extension is (like for struts it is usually *.do).

CoolBeans
+2  A: 

The filter won't be invoked when:

  1. The filter class is missing in the classpath and/or is not loadable or instantiable. You should however have noticed it in the server's startup logs. Solution is to be found based on the interpretation of the exceptions/errors found in the server logs.

  2. There's another filter running before in the chain which isn't calling FilterChain#doFilter(), but rather RequestDispatcher#forward() or include() which caused the subsequent filters in the chain being completely skipped (when they do not listen on FORWARD or INCLUDE dispatchers; they by default only listens on REQUEST dispatcher). Solution is either to fix the wrong filter, or to add <dispatcher>FORWARD</dispatcher> etc accordingly, or to rearrange the filter declarations in web.xml so that your new filter comes before the another filter (you in turn only need to ensure that your new filter is using the FilterChain#doFilter() properly :) ).

  3. The request URL is plain wrong. You used http://localhost:8080/hello/world. With a filter listening on /*, this means that the webapp context should be ROOT or at least /hello. Verify your webapp context. I'd just retry with an URL which points to a valid JSP/Servlet inside the same webapp which generates a non-404 response. Does the filter then get called as well?

BalusC
1. Filter class is good since initFilterBean is called properly.
e.b.white
2. I have updated web.xml, please check.
e.b.white
I check the destroy() API:This method is only called once all threads within the filter's doFilter method have exited or after a timeout period has passed. Maybe it is timeout?
e.b.white
I added a 3rd cause (which is after all pretty obvious, but you never know :) ).
BalusC
I think so too BalusC. As I said in my response, his filter is probably set on the wrong URL.
CoolBeans
@Coolbeans: your answer did however not explain it that way and is suggesting a bit odd solution.
BalusC
Haha, I see, it works, thanks!
e.b.white
I agree BalusC. My answer was not explained in details.
CoolBeans
@zhang: if an answer helped in solving the problem, you should mark it accepted. I'd suggest to also review the answers on all [other questions](http://stackoverflow.com/users/149199/zhangzhong) you asked previously. As of now you've an accept rate of `8%` which is pretty poor.
BalusC