☰ See All Chapters |
Servlet Filters
Filters are used to filter the request and response from servlet. We can access and update the HttpServletRequest before it reaches the servlet and can access and update the HttpServletResponse objects before they are passed to the client. Filters can be used to track the users IP from all incoming requests and logs the IP addresses of the computers from which the requests originate. Filters can be used to decrypt the request if it is encrypted from client and can be used to encrypt the response to allow it to pass throw channels. Filters can be used as user input validators as a server side validation.
Filters API
Following are the three important filter related interfaces in the javax.servlet package:
Filter
FilterConfig
FilterChain
Filter Interface
To create filter we have to implement javax.servlet.Filter interface and have to implement the following three life cycle methods.
public void init(FilterConfig filterConfig) public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) public void destroy() |
After instantiating filter init() method will be called and will be called only once. Inside doFilter() method we write filter code. The servlet container calls the doFilter() method every time a user requests a servlet, to which the filter is mapped. As we can see doFilter() method has request and response parameters. We can use these parameters and we can intercept and manipulate.
Inside destroy() method we can write code to do cleanup operations like close the connections, resources which opened by filter.
The FilterConfig Interface
FilterConfig object can be used to obtain configuration values like context parameters form web.xml
The FilterConfig interface has four methods:
public String getFilterName(): Returns the name of the filter
public String getInitParameter(String parameterName): Returns the value of servlet context parameter
public java.util.Enumeration getInitParameterNames(): Returns Enumeration containing all parameter names of the filter
public ServletContext getServletContext():Returns the ServletContext objectThe FilterChain Interface
The FilterChain Interface
doFilter() method of the filter class has the FilterChain parameter. Filters use the FilterChain object to invoke the next filter in the chain, or, if the filter is the last in the chain, to invoke the next resource (servlet). The FilterChain interface has only one method: doFilter, whose signature is given as follows:
public void doFilter(HttpServletRequest request, HttpServletResponse response)
When compared to the doFilter() method of Filter interface, we have only two parameters in doFilte() of FilterChain interface.
Servlet Filter Example
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 FilterDemo implements Filter { private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter initialized"); this.filterConfig = filterConfig; }
public void destroy() { System.out.println("Filter destroyed"); this.filterConfig = null; }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("Inside doFilter"); chain.doFilter(request, response); } } |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>demoFilter</filter-name> <filter-class>com.java4coding.FilterDemo</filter-class> </filter> <filter-mapping> <filter-name>demoFilter</filter-name> <servlet-name>demoServlet</servlet-name> </filter-mapping> <servlet> <servlet-name>demoServlet</servlet-name> <servlet-class>com.java4coding.DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>demoServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
Applying Filter to more than one servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>demoFilter</filter-name> <filter-class>com.java4coding.FilterDemo</filter-class> </filter> <filter-mapping> <filter-name>demoFilter</filter-name> <servlet-name>demoServletOne</servlet-name> </filter-mapping> <filter-mapping> <filter-name>demoFilter</filter-name> <servlet-name>demoServletTwo</servlet-name> </filter-mapping> <servlet> <servlet-name>demoServlet</servlet-name> <servlet-class>com.java4coding.DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>demoServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
Mapping Filter to URL
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <filter> <filter-name>demoFilterOne</filter-name> <filter-class>com.java4coding.FilterDemo</filter-class> </filter> <filter-mapping> <filter-name>demoFilterOne</filter-name> <url-pattern>/servlet/ServletWithFilter</url-pattern> </filter-mapping> <filter> <filter-name>demoFilterTwo</filter-name> <filter-class>com.java4coding.FilterDemo</filter-class> </filter> <filter-mapping> <filter-name>demoFilterTwo</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>demoServlet</servlet-name> <servlet-class>com.java4coding.DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>demoServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
All Chapters