What is Filter
A filter is an object that is invoked at the preprocessing and postprocessing of a request.

It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc.

The servlet filter is pluggable, i.e. its entry is defined in the web.xml file, if we remove the entry of filter from the web.xml file, filter will be removed automatically and we don’t need to change the servlet.

Filter interface
Filter interface contains three methods

  1. public void init(FilterConfig config)init() method is invoked only once. It is used to initialize the filter.
  2. public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain)doFilter() method is invoked every time when user request to any resource, to which the filter is mapped.It is used to perform filtering tasks.
  3. public void destroy()This is invoked only once when filter is taken out of the service.

How to define a Filter
web.xml

<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>  

Example of Filter
web.xml


<web-app>
        <servlet>
		<servlet-name>AdminServlet</servlet-name>
		<servlet-class>AdminServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>AdminServlet</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>  

MyFilter.java

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

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>");
	}
}

Whenever the /Servlet1is accessed it will go through the filter before and after processing the actual java page HelloServlet.java

How to access Init param in filter
web.xml

.
<filter>
	<filter-name>f1</filter-name>
	<filter-class>MyFilter</filter-class>
	<init-param>
		<param-name>construction</param-name>
		<param-value>no</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>f1</filter-name>
	<url-pattern>/servlet1</url-pattern>
</filter-mapping>  
.
.

MyFilter.java

public class MyFilter implements Filter 
{
	FilterConfig config;

	public void init(FilterConfig config) throws ServletException 
	{
		this.config = config;
	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {

		PrintWriter out = resp.getWriter();

		String s = config.getInitParameter("construction");

		if (s.equals("yes")) {
			out.print("This page is under construction");
		} else {
			chain.doFilter(req, resp);// sends request to next resource
		}
	}

	public void destroy() {
	}
}

Comments are closed.