- Write a Java class that implements the Filter interface and override filter’s life cycle methods.
- Specify initialization parameters for the filter (optional).
- Specify filter mapping, either to Java servlets or URL patterns.
Table of content:2. Specify Initialization Parameters for Filter
4. How to create a servlet filter in Eclipse IDE
5. How to create a servlet filter in NetBeans IDE
Let’s see each step in detailed.package net.codejava; 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; public class HelloFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // invoked when an instance of this filter is created by the container // used to initialize resources, read parameters... } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // invoked when a matching request sent to the server // used to intercept the request and transform the response chain.doFilter(request, response); // invokes next filter in the chain } @Override public void destroy() { // invoked when the filter instance is being destroyed by the container // used clean up resources } }As you can see, this class must implement the Filter interface and override its three life cycle methods: init(), doFilter() and destroy(). The purpose of each method is clearly explained in the comments. Read this article to learn more about the annotation type @WebFilter.
package net.codejava; import java.io.IOException; import javax.servlet.*; public class HelloFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // invoked when a matching request sent to the server // used to intercept the request and transform the response chain.doFilter(request, response); // invokes next filter in the chain } }
<filter> <filter-name>Hello Filter</filter-name> <filter-class>net.codejava.HelloFilter</filter-class> <init-param> <param-name>message</param-name> <param-value>A message from Hello Filter</param-value> </init-param> <init-param> <param-name>threshold</param-name> <param-value>1024</param-value> </init-param> </filter>As you can see, you must specify the filter name and its fully qualified class name:
<filter-name>Hello Filter</filter-name> <filter-class>net.codejava.HelloFilter</filter-class>An each parameter is declared like this:
<init-param> <param-name>threshold</param-name> <param-value>1024</param-value> </init-param>You can specify as many parameters as you want. Then in the filter’s init() method you can read these initialization parameters via the FilterConfig object like this:
public void init(FilterConfig fConfig) throws ServletException { String message = fConfig.getInitParameter("message"); String threshold = fConfig.getInitParameter("threshold"); // do something with the parameters }NOTE: You can use annotations to specify initialization parameters right inside the filter class, but it is not recommended because that requires re-compiling the code when you want to change the values of the parameters.
<filter-mapping> <filter-name>Hello Filter</filter-name> <servlet-name>Hello Servlet</servlet-name> </filter-mapping>The servlet is declared and mapped in the web.xml file as follows:
<servlet> <servlet-name>Hello Servlet</servlet-name> <servlet-class>net.codejava.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello Servlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>If the servlet class is annotated using @WebServlet annotation, you must specify the name of the servlet like this:
@WebServlet(name = "Hello Servlet", urlPatterns = {"/hello"}) public class HelloServlet extends HttpServlet { }You can map a filter to a URL pattern like this:
<filter-mapping> <filter-name>Hello Filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>Here, the filter Hello Filter is mapped to the URL pattern /*, which means it will intercept all the requests.And the following example illustrates how to map a filter to multiple URL patterns:
<filter-mapping> <filter-name>Hello Filter</filter-name> <url-pattern>/hello</url-pattern> <url-pattern>/bonjour</url-pattern> <url-pattern>/ciao</url-pattern> </filter-mapping>
import javax.servlet.annotation.WebFilter; // other imports @WebFilter("/hello") public class HelloFilter implements Filter { // implement life cycle methods… }You can see this filter class is mapped to the URL pattern /hello - which is more convenient than using XML, right?The following example shows how to map a filter to multiple URL patterns:
@WebFilter({ "/servlet1", "/servlet2", "/servlet3" }) public class FilterA implements Filter { }And this example illustrates how to map a filter to a servlet using annotation:
@WebFilter(servletNames = { "Hello Servlet" }) public class HelloFilter implements Filter { }Note that you can map one filter to one or more serlvets/URL patterns, and map multiple filters to one servlet/URL pattern.