Filter API

The Filter API
The Filter API consists of follwoing three interfaces :
  1. javax.servlet.Filter           
  2. javax.servlet.FilterConfig           
  3. javax.servlet.FilterChain           
The Filter interface
public interface Filter : A filter implements the interface javax.servlet.Filter. This interface encapsulates the lifecycle method of a filter.
Method of Filter interface
  1. public void init(FilterConfig config)
              The containe invokes this method before putting the filter into services. This method invokes only once. During initilization, the container sends the configuration information to the filter using FilterConfig object.
  2. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
              This container invokes this method while processing a filter.
  3. public void destroy()
              The container invokes this method before taking the filter out of service.
The FilterConfig interface
public interface FilterConfig : This interface is the equilvalent of ServletConfig and its is used for filters. The interface provides access to the filter.
Method of FilterConfig interface
  1. public void getFilterName():
               This method returns the name of filter as assgined in deployment descriptor.
  2. public String getInitParamter(String name):
              This method returns the value of the named initilization parameters or null if the parameter does not exist.
  3. public Enumeration getInitParamterNames():
              This method returns the name of Servlet initilizatiion parameters as an Enumeration of String objects, or an empty Enumeratio of the Servlet has no initilizaion parameters.
  4. public String getServletContex():
               This method returns a reference to the ServletContext object associated with the web application.
The FilterChain interface
public interface FilterChain : This interface provides the filter with a handle to invoke the rest of the filter chain. Each filter gets access to a FilteChain object when its doFilter() method is invoked. Using the object of FilterChain, the filter invokes the next filter in the chain .
Method of FilterChain interface
The FilterChain interface has only one method.
  1. public void doFilter(ServletRequest request, ServletResponse response ):
               This method invokes the next filter in the filter chain.
index.html
<html> <head> <title>Cookie Demo Servlet </title> </head> <body> <form action="FirstServlet" method="post"> User Name <input type="text" name="userName"> &nbsp; <input type="submit" value="Submit"> <br> </form> </body> </html>
FirstServlet.java
import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String uname=request.getParameter("userName"); out.print("Welcome "+uname); //Creating Cookie Object Cookie cookie=new Cookie("uname",uname); //Adding Cookie object in the response response.addCookie(cookie); out.print("<form action='SecondServlet' method='post'>"); out.print("<input type='submit' value='Login'>"); out.print("</form>"); out.close(); } catch(Exception e){System.out.println(e);} } }
SecondServlet.java
import java.io.PrintWriter; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SecondServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response){ try{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Cookie cookie[]=request.getCookies(); out.print("Cookies Value "+cookie[0].getValue()); out.close(); } catch(Exception e){ System.out.println(e); } } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app > <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>SecondServlet</servlet-name> <servlet-class>SecondServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SecondServlet</servlet-name> <url-pattern>/SecondServlet</url-pattern> </servlet-mapping> </web-app>
Output
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE
 



 

Latest Tutorial


Most Visited Tutorial