Servlet Introduction
Servlet (Example-1)
Life Cycle Events
Con.Ini.Par.(Example-2)
Lif.Cyc.Evt.(Example-3)
Err. & Exce.(Example-4)
Filters (Example-5)
Servlet Hints
Bookmark This Site
|
Servlet Filters
Introduction
|
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.*;
-
import java.io.*;
-
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;
-
}
-
}
Download ProcessingTimeFilter.java
|
Deploying Servlet Filters
|
-
Write a java program and name it as ProcessingTimeFilter.java
-
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;
(OR) Set the path in the system itself. CLICK HERE for details
-
Now compile the ProcessingTimeFilter.java. CLICK HERE to see how to compile
-
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
-
Goto Start->Programs-> Sun Microsystems->Application Server PE->Deploytool. CLICK HERE to see how to Start the Deploytool
- Goto File ->New -> Application Note: Inserted of EmployeeDetails use Filter
(Click the Browse button)
-
(Select the folder in the Look In dropdown box, and then give a file name “Filter”. Next click the New Application button)
(Click the OK button)
- Now goto File -> Save to save the file
- Next, goto File -> New -> Web Component
(Click Next button)
(Enter the WAR Name as “EmpApp” and then click the Edit Contents… button)
(Select all the .class, .jsp , .tld and .html files and click the Add button)
(Now click the OK button)
(Now click the Next button)
- Now select the No Component option button and then click the Next button
(Now select the Finish button)
(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)
- 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)
- 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)
(Now click the Add button)
- 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)
- Now goto File ->Save
- Next goto Tools -> Deployee
(Enter the User Name as “admin” and Password as “password” (CLICK HERE for password). Next click the OK button)
(Now a message --- Operation Completed Successfully --- must display. Next click the Close button)
- Next goto File -> Exit to close it
- 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)
- The log file details can be taken from the server.log file
(E:\Sun\AppServer\domains\domain1\logs\server.log)
- Program completed Successfully
- 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
|
|
|