• Servlet filters were first introduced in the Servlet 2.3 Specification.

  • It used for some additional processing and transformation of data present in Request and Response Objects.

  • Filters allow you to add for performing some business processing before or after the servlet in Java classes.

  • It is easily done without changing any Servlet Code by only small modification of deployment descriptor element placed in Web.xml file
    The package javax.servlet provides three Interfaces:-
  1. Filter.
  2. FilterConfig.
  3. FilterChain.
If You want to create filter so you write Java Class that implement javax.servlet.Filter.

Methods Description
doFilter(ServletRequest req,
ServletResponse res,
FilterChain chain)
Calls by Container when Client request a servlet that has this particular filter placed in web.xml.
If to pass these objects to the next filter in the chain,use third parameter.
init(FilterConfig filtConfig) Call by Container When filter is loaded.
destroy() Calls when the filter is unloaded.
FilterConfig provides an access to the initialization parameters of the servlet and to the object ServletContext.

Method Description
String getInitParameter(String name) Returns a String that has value of the named initialization parameter.nameid is specify a parameter name.
Enumeration getInitParameterNames() Returns the names of the filter's initialization parameters
String getFilterName() Returns the filter-name of this filter,defined in Web.xml.
ServlteContext getServletContext() Returns Object of Servlet Context used to interact with its Container.
FilterChain use to invoke the next filter upto last filter in chain and this method sends the output back to the user's Web browser.
Method Description
public void doFilter(
ServletRequest req,
ServletResponse resp)
throws IOException, ServletException
Object of ServletRequest(req) to pass along the chain. Object of ServletResponse(resp)response to pass along the chain.