Enterprise Beans

Contact Us

Servlet Filters


The following figure shows a group of filters, filtering the requests and responses for different servlets in a Web application:

The servlet API provides the Filter interface, FilterConfig interface, and FilterChain interface of the javax.servlet package that you can use to develop filters. To develop a filter, you need to implement the Filter interface in your filter class.

Example for Servlet Filters

CLICK HERE to download this complete example (zip file)

 Creating Filters

import javax.servlet.*;
import javax.servlet.http.*;
public class ProcessingTimeFilter implements Filter
FilterConfig flt_cnfg = null;
public void init(FilterConfig f_cnfg) throws ServletException
this.flt_cnfg = f_cnfg;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
long service_Start = System.currentTimeMillis();
chain.doFilter(request, response);
long service_Stop = System.currentTimeMillis();
long serviceTime = (service_Stop - service_Start);
String path = ((HttpServletRequest)request).getRequestURI();
flt_cnfg.getServletContext().log("Time taken to process request for: "+path+" is: "+serviceTime+ " milliseconds");
public void destroy ()
this.flt_cnfg = null;


 Deploying Servlet Filters
  1. Write a java program and name it as
  2. Set the path in the command prompt
    set path=.;C:\progra~1\java\j2sdk1.5.0\bin;C:\Sun\AppServer\bin;
    Set classpath=.;C:\progra~1\java\j2sdk1.5.0\lib;C:\Sun\AppServer\lib\j2ee.jar;
    Set the path in the system itself. CLICK HERE for details
  3. Now compile the CLICK HERE to see how to compile
  4. Goto Start->Programs->Sun Microsystems->Application Server PE->Start Default Server (Wait till it start and then press any key). CLICK HERE to see how to Start the Server
  5. Goto Start->Programs-> Sun Microsystems->Application Server PE->Deploytool. CLICK HERE to see how to Start the Deploytool
  6. Goto File ->New -> Application     Note:    Inserted of EmployeeDetails use Filter

    (Click the Browse button)

  7. (Select the folder in the Look In dropdown box, and then give a file name “Filter”. Next click the New Application button)

  8. (Click the OK button)
  9. Now goto File -> Save to save the file
  10. Next, goto File -> New -> Web Component

    (Click Next button)

  11. (Enter the WAR Name as “EmpApp” and then click the Edit Contents… button)

  12. (Select all the .class, .jsp , .tld and .html files and click the Add button)

  13. (Now click the OK button)

  14. (Now click the Next button)
  15. Now select the No Component option button and then click the Next button

  16. (Now select the Finish button)

  17. (Now select the EmpApp in the left pane and select the General tab in the right pane. Here give a name “example5” in the Context Root text box)
  18. Now select the EmpApp in the left pane and then select the Filter Mapping tab in the right pane

    (Now click the Edit Filter List button)

  19. Now click the Add Filter button and then select ProcessingTimeFilter in the Filter Class drop-down box. Next give a Filter Name as ProcessingTimeFilter

    (Now click the OK button)

  20. (Now click the Add button)
  21. Next, select the Filter Name from the drop-down box and next in the Filter Target pane select the Filter Servlets that match this URL Pattern option and specify /* in the URL Pattern

    (Now click the OK button)

  22. Now goto File ->Save
  23. Next goto Tools -> Deployee

    (Enter the User Name as “admin” and Password as “password” (CLICK HERE for password). Next click the OK button)

  24. (Now a message --- Operation Completed Successfully --- must display. Next click the Close button)
  25. Next goto File -> Exit to close it
  26. Now open an Internet Explorer and type the address http:// localhost:8080/example5

    (Note: When this Context-name is called immediately the filter will be activated. We can use log files to store the details of the time started, used time, accessing client side details, etc)
  27. The log file details can be taken from the server.log file

  28. Program completed Successfully
  29. To stop the server goto Start -> All Programs -> Sun Microsystems -> Application Server PE -> Stop Default Server. CLICK HERE to see how to Stop the Server

 Click for Next Topic