Filter in Servlet

J2EE application components are container-managed. When a client makes a request to J2EE component, such as servlet, jsp page, or EJB, the container receives the requests and then invokes the appropriate component instances managed in the container runtime. In the case of the web container, it receives HTTP request and invokes the various web application resources such as Servlet, JSP page, HTML pages, files , images and so on. That is part of web container interceptor for HTTP request ot resources.
The interceptor is used for identifying the web application, finding path mapping to resources, checking for authentication and authorization and invoking resources. There is no facility to paricipate in this interception process. We cannot chage, control or peak the container invokes resources.
What is filter ?
We need a facility to pulg in some logic during the HTTP request handling process. Filters proviedes the mechanism by which we can plug in code to paricipate in the container interception process.
Filters are an under-appreciated feature of the Java servlet platform, ideal for writing components that can be added transparently to any web application. A filter is like a lightweight servlet that doesn't generate its own content, instead it plugs into the request handling process and executes in addition to the normal page processing.
Uses of Filter
  • Filters might record information about requests
  • convert content to a different format
  • redirect access to a different page
  • Filters can be applied to any resources served by a servlet engine, whether it's flat HTML, graphics, a JSP page, servlet , or whatever
  • They can be added to an existing web application without either the filter or the application being aware of one another.
The Filter API
The Filter API consists of follwoing three interfaces :
  1. javax.servlet.Filter           
  2. javax.servlet.FilterConfig           
  3. javax.servlet.FilterChain           
Simple Filter Example
<html> <head> <title>Simple Filter Demo </title> </head> <body> <center> <form action="WelcomeServlet" method="post"> <fieldset> <legend> <font face="Courier New" size="+1" color="red"> Login </font> </legend> <table> <tr> <td>User Name </td> <td> <input type="text" name="userName"> </td> </tr> <tr> <td>Password </td> <td> <input type="text" name="password"> </td> </tr> <tr> <td colspan=2> <input type="submit" value="Login"> </td> </tr> </table> </fieldset> </form> </center> </body> </html>
import; import; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class FirstFilter implements Filter { public void init(FilterConfig fConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); out.println("Welcome to My First Filter"); out.print("<BR>"); chain.doFilter(request, response); } public void destroy() { } }
import; import; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WelcomeServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("userName"); String password=request.getParameter("password"); out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Welcome Page </TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("User Name is "+name); out.println("<BR>"); out.println("Password is "+password); out.println("<P>Welcome to the</P>"); out.println("</BODY>"); out.println("</HTML>"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5"> <display-name>FilterDemo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <display-name>FirstFilter</display-name> <filter-name>FirstFilter</filter-name> <filter-class>FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/WelcomeServlet</url-pattern> </filter-mapping> <servlet> <display-name>WelcomeServlet</display-name> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> </web-app>
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE


Latest Tutorial

Most Visited Tutorial