Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a servlet filter programmatically?

Although I've seen many similar questions, I didn't find a clear answer. Using Servlet Spec 2.5, is it possible to add servlet filters and mappings programmatically? The preferred location would be in a Servlet.init() or in ServletContextListener.contextInitialized().

like image 472
Zeemee Avatar asked Oct 26 '11 14:10

Zeemee


People also ask

What is the correct syntax to configure filters in servlets?

public void init(FilterConfig filterConfig) This method is called by the web container to indicate to a filter that it is being placed into service.

What is doFilter () method in Java?

The doFilter method of the Filter is called by the container each time a request/response pair is passed through the chain due to a client request for a resource at the end of the chain. The FilterChain passed in to this method allows the Filter to pass on the request and response to the next entity in the chain.

How does servlet filter work?

How does Servlet Filter work? When a request is made, it reaches the Web Container and checks if the filter contains any URL pattern, which is similar to the pattern of the URL requested. The Web Container locates the very first filter which matches the request URL, and then that filter code is executed.


1 Answers

No, not by the standard Servlet 2.5 API. This was introduced in Servlet 3.0. Your best bet is to create a single filter and reinvent the chain of responsibility pattern yourself. An alternative is to grab container specific classes from under the covers and then add the filter by its API. How exactly to do that depends on the target container (and it would also make your code tight coupled to the container in question).

See also:

  • How to add filters to servlet without modifying web.xml

Update: as requested by comment, here's an example in flavor of a ServletContextListener how you could add filters programmatically during webapp's startup using Tomcat 6 specific APIs:

package com.example;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.catalina.Container;
import org.apache.catalina.ServerFactory;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.deploy.FilterDef;
import org.apache.catalina.deploy.FilterMap;

public class Tomcat6FilterConfigurator implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        StandardEngine engine = (StandardEngine) ServerFactory.getServer().findService("Catalina").getContainer();
        Container container = engine.findChild(engine.getDefaultHost());
        StandardContext context = (StandardContext) container.findChild(event.getServletContext().getContextPath());

        FilterDef filter1definition = new FilterDef();
        filter1definition.setFilterName(Filter1.class.getSimpleName());
        filter1definition.setFilterClass(Filter1.class.getName());
        context.addFilterDef(filter1definition);

        FilterMap filter1mapping = new FilterMap();
        filter1mapping.setFilterName(Filter1.class.getSimpleName());
        filter1mapping.addURLPattern("/*");
        context.addFilterMap(filter1mapping);

        // ...
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // TODO Auto-generated method stub

    }

}

Register this listener as follows in web.xml:

<listener>
    <listener-class>com.example.Tomcat6FilterConfigurator</listener-class>
</listener>

Once again, keep in mind that this does not work on containers of other make/version, even not on Tomcat 7.0.

like image 135
BalusC Avatar answered Oct 14 '22 22:10

BalusC