Java URL Filter Pattern Specific to Request Parameters

We have a situation where we want to use a filter for a URL containing some specific request parameters, for example:

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

etc., id are retrieved at runtime, I want to configure the filter in web.xml only if formtype=special_form . How to reach a solution? Can I customize a filter using regex patterns?

+4
source share
2 answers

As far as I know, there is no solution for matching queries to filters by query string directly in web.xml . Thus, you can register the filter in web.xml using the init-param parameters to configure the filter and set the template 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 will 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

+6
source

You should parse the URL parameters in the filter body, not in web.xml;

I did such a thing, but I used a phase listener, a configuration record (in the configuration file), which matched the URL parameters for the FQN classes that handle a certain β€œaction” (by action I mean the URL parameter from GET / POST); than, the phase listener will analyze all URL parameters from each GET / POST and send each of them to the dispatcher. The dispatcher's task is to call the right handler (the singleton object corresponding to the FQN for this URL parameter). The handler then does the specific thing with the URL value that it receives.

The same can be done using a filter instead of a phase listener.

0
source

Source: https://habr.com/ru/post/1305199/


All Articles