Filter Servlet in Java

Filter

  
It is a program which executes on the time of responding to a request to a specific  URL. The filter resides in a webserver. Whenever the user makes the request to the associated URL then the request goes to the filter. The filter can modify the request or perform any task before passing the request to the destination.
 
Creation process of a filter
  1. Create a class by inheriting javax.servlet.filter interface.
  2. Override the methods of the filter interface. The methods are as follows: 
    public void init()
    public void doFilter(servletRequest req,servletResponse res,filterchain chain)
    public void destroy() 
  3. Perform the required task inside doFilter()
  4. Transfer request and response to the destination by using doFilter() of filterchain.
Storing, compilation and deployment of filter
  1. Store the filter file inside classes' folder of the context.
  2. Compile the filter file by using servlet-api.jar in classpath.
  3. Deploy the filter by using the required tags in web.xml file. The tags are as follows:
    1. <filter>  
    2. <filter-name>This contains mapping name for the filter</filter-name>  
    3. <filter-class> This contains class name for the filter </filter-class>  
    4. </filter>  
    5.   
    6. <filter-mapping>  
    7. <filter-name> This contains mapping name for the filter </filter-name>  
    8. <url-pattern>/ This contains the url for whom the filter will execute </url-pattern>  
    9. </filter-mapping>  
Example
  
access.html
  1. <html>  
  2. <body>  
  3. <h1><a href="./genserv">Visit Genserv</a></h1>  
  4. </body>  
  5. </html>  
genserv.java file 
  1. //Using GenericServlet    
  2. import java.io.*;    
  3. import javax.servlet.*;    
  4. public class genserv extends GenericServlet     
  5. {    
  6.  public void service(ServletRequest req, ServletResponse res) throws IOException, ServletException     
  7.  {    
  8.   PrintWriter out = res.getWriter();    
  9.   res.setContentType("text/html");    
  10.   out.println("<html><body bgcolor='blue'>");    
  11.   out.println("<h1>First Servlet</h1>");    
  12.   out.println("</body></html>");    
  13.  }    
  14. }   
Filtercounter.java file 
  1. //A filter file  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. import java.io.*;  
  5.   
  6. public class Filtercounter implements Filter   
  7. {  
  8.  public void init(FilterConfig con) {}  
  9.  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException   
  10.  {  
  11.   try   
  12.   {  
  13.    FileWriter fw = new FileWriter("E:/filter/fil.txt"true);  
  14.   
  15.    fw.write("page accessed");  
  16.    fw.close();  
  17.    chain.doFilter(request, response);  
  18.   }   
  19.   catch (Throwable t)   
  20.   {  
  21.    System.out.println(t);  
  22.   }  
  23.  }  
  24.  public void destroy() {}  
  25. }  
Web.xml settings 
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <!--  
  3. Licensed to the Apache Software Foundation (ASF) under one or more  
  4. contributor license agreements. See the NOTICE file distributed with  
  5. this work for additional information regarding copyright ownership.  
  6. The ASF licenses this file to You under the Apache License, Version 2.0  
  7. (the "License"); you may not use this file except in compliance with  
  8. the License. You may obtain a copy of the License at  
  9.   
  10. http://www.apache.org/licenses/LICENSE-2.0  
  11.   
  12. Unless required by applicable law or agreed to in writing, software  
  13. distributed under the License is distributed on an "AS IS" BASIS,  
  14. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15. See the License for the specific language governing permissions and  
  16. limitations under the License.  
  17. -->  
  18. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  19. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  20. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  21. version="2.5">  
  22.   
  23. <filter>  
  24. <filter-name>Filtercounter</filter-name>  
  25. <filter-class>Filtercounter</filter-class>  
  26. </filter>  
  27.   
  28. <filter-mapping>  
  29. <filter-name>Filtercounter</filter-name>  
  30. <url-pattern>/genserv</url-pattern>  
  31. </filter-mapping>  
  32.   
  33. <servlet>  
  34. <servlet-name>genserv</servlet-name>  
  35. <servlet-class>genserv</servlet-class>  
  36. </servlet>  
  37.   
  38. <servlet-mapping>  
  39. <servlet-name>genserv</servlet-name>  
  40. <url-pattern>/genserv</url-pattern>  
  41. </servlet-mapping>  
  42.   
  43. </web-app>  
Compile both the files as below 
 
javac -cp servlet-api.jar genserv.java (for tomcat 6.0)
 
javac -cp servlet-api.jar Filtercounter.java (for tomcat 6.0) 
 
filter in servlet  
 
Output 
 
Run the tomcat then write the below line in the URL 
 
Here the test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory. 
 
http://localhost:8081/test/ 
 
After giving the URL a set a listing will come, here only one appears 
 
As access.html, click it 
 
filter in servlet
 
Note:- You will notice in E:/filter/fil.txt file how many times the user will visit this Url(http://localhost:8081/test/genserv) that the number of times page access will be written to that file.