A filter is an item that is invoked on the preprocessing and postprocessing of a request.
It is particularly used to carry out filtering responsibilities including conversion, logging, compression, encryption and decryption, input validation etc.
The servlet filter is pluggable, i.e. its access is described withinside the web.xml file, if we eliminate the access of clear out out from the web.xml file, clear out out might be eliminated routinely and we do not want to extrade the servlet.
So the upkeep cost might be less.
Note-Contrary Servlet, One filter doesn’t have dependency on any other filter.
Like servlet filters have their own API. There are three interfaces of Filter API contained by the javax.servlet package.
For creating any filter, you want to implement the Filter interface. The life cycle methods for a filter are provided by Filter interface.
Method | Description |
public void init(FilterConfig config) | init() way is supplicate only once. It is used to initialize the filter. |
public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain) | doFilter() method is supplicate every time when a user requests any resource, to which the filter is mapped. It is used to execute filtering tasks. |
public void destroy() | This is supplicate only once when filter is taken out of the service. |
The object of FilterChain is responsible to invoke subsequent filter or resource within the chain.This object is passed within the doFilter method of Filter interface.The FilterChain interface contains just one method:
We can define filters the same as servlets. Let's examine the weather of filter and filter-mapping.
<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>
Either Url-Pattern or Servlet-name both can be used for mapping filters. The Url-Pattern component has a benefit over Servlet-name components, that is it can be applied on servlet, JSP or HTML.
We are easily showing information that filter is supplicate automatically once after the post processing of the request, in the below given example:
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("
welcome to servlet
"); } }
Web.xml
Filter element of the web-app must be defined just like a servlet, For defining the filter.
<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>
download this example (developed using Myeclipse IDE)