3

We have a situation where we want to use filter for URL's containing some specific request parameters, e.g:

http://mydomain.com/?id=78&formtype=simple_form&.......    
http://mydomain.com/?id=788&formtype=special_form&.......    

and so on, id are fetched at run time, I want configure filter in web.xml only if formtype=special_form. How should achieve the solution? Can Filter be configured with regex patterns?

2
  • Why do you want to do it in filter mapping? It's not possible to use anything else than "*" and almost anything can be checked in filter's doFilter method anyway. The only condition where regexp filter mapping would be useful is when you don't have access to the filter's source. Is this your case? Commented Mar 26, 2010 at 8:40
  • You have to see some really bad code, but it's out there. I have a java web app I inherited that I'll need to rewrite. There are no servlets! You access the jsps directly, outside of the web-inf directory, so I had to write a filter to intercept and block certain url requests. Commented Apr 18, 2013 at 14:28

2 Answers 2

6

As far as I know there is no solution for matching requests to filters by query string directly in web.xml. So you could register the filter in your web.xml using init-params to make the filter configurable and set a pattern via void init(FilterConfig filterConfig) in your javax.servlet.Filter implementation.

package mypackage;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class MyFilter implements Filter {

    private String pattern;

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        // check whether we have a httpServletRequest and a pattern
        if (this.pattern != null && request instanceof HttpServletRequest) {
            // resolve the query string from the httpServletRequest
            String queryString = ((HttpServletRequest) request).getQueryString();
            // check whether a query string exists and matches the given pattern
            if (queryString != null && queryString.matches(pattern)) {
                // TODO do someting special
            }
        }
        chain.doFilter(request, response);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.pattern = filterConfig.getInitParameter("pattern");
    }

}

The configuration would look like this in your web.xml:

<!-- MyFilter -->
<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>mypackage.MyFilter</filter-class>
    <init-param>
        <param-name>pattern</param-name>
        <param-value>{{PATTERN HERE}}</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Further readings:
http://java.sun.com/javaee/5/docs/api/javax/servlet/Filter.html

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, your answer really worked like a magic for me. Really appreciate your help. Thanks, Rohit.
A filter which supports patterns as known from Apache mod_rewrite (the RewriteRule stuff and so on) is available here: tuckey.org/urlrewrite
0

You must parse the URL params in the body of the filter, not in web.xml;

I made such a thing, but I used a phase listener, a configuration entry (in a configuration file) that mapped URL params to FQN of classes that handle a particular "action" (by action I mean a URL param from a GET/POST); than, the phase listener would parse all the URL params from each GET/POST and send each of them to a Dispatcher. The Dispatcher's job is to call the right handler (singleton object corresponding to the FQN for that URL param). The handler then just does the specific thing with the URL value he receives.

The same thing can be made by using a filter instead of phase listener.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.