A filter can be defined as an object that is invoked at the time of pre-processing and post-processing of the request. It is mostly used to perform filtering tasks such as conversion, compression, logging, input validation encryption and decryption, etc.
Servlet filter is pluggable, i.e. filter´s entry is defined in web.xml file, if we remove the entry of filter from the web.xml file, It will be removed automatically and we don´t need to make any changes to the servlet.
Note: Unlike Servlet, One filter doesn´t have any kind of dependency on another filter.
Filter API: Like servlet filter also have its own API. The javax.servlet package have three interfaces of Filter API.
1. Filter
2. FilterChain
3. FilterConfig
1) Filter interface:
--------------------------
For creating any filter, you must need to implement the Filter interface. Filter interface provides the life cycle methods for a filter.
public void init(FilterConfig config): is invoked only once to initialize the filter.
public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain): is invoked every time when the user requests to any resource, to which the filter is mapped. It is used to perform filtering tasks described as above.
public void destroy(): is invoked only once when filter is taken out of the service.
2) FilterChain interface:
------------------------------------
The object of FilterChain is responsible to invoke the next filter or resource in the chain. This object is passed as a parameter in the doFilter method of Filter interface. The FilterChain interface contains only one method:
public void doFilter(HttpServletRequest request, HttpServletResponse response): it is responsible to pass the control to the next filter or resource.
How to define Filter in web.xml:
----------------------------------------------
We can define filter same as servlet.
<web-app>
<filter>
<filter-name>...</filter-name>
<filter-class>...</filter-class>
</filter>
<filter-mapping>
<filter-name>...</filter-name>
<url-pattern>...</url-pattern>
</filter-mapping>
</web-app>
For mapping filter we can use, either url-pattern or servlet-name. The url-pattern elements has an advantage over servlet-name element i.e. it can be applied on servlet, JSP or HTML.
Let´ see the Simple Example of Filter:
------------------------------------------------------
In this example, we are displaying some information that filter is invoked automatically after the post processing of the request.
index.html:
-------------------
<a href="servlet1">click here</a>
MyFilter.java:
---------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class MyFilter implements Filter{
public void init(FilterConfig arg0) throws ServletException {}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
PrintWriter out=resp.getWriter();
out.print("filter is invoked before");
chain.doFilter(req, resp);//sends request to next resource
out.print("filter is invoked after");
}
public void destroy() {}
}
HelloServlet.java:
---------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<br>welcome to servlet<br>");
}
}
web.xml:
---------------
For defining the filter, filter element of web-app must be defined just like servlet.
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<filter>
<filter-name>f1</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/servlet1</url-pattern>
</filter-mapping>
</web-app>
5