Java Servlet Filters :
Servlet filters are the classes which can intercept the request and can modify the response.
Filters are not servlets, because filters can not create the response.The purpose of using
filters are as follows :
To intercept requests coming from a client before process it at back end.
To modify the request headers and request data if needed.
To manipulate responses coming from server before they are sent back to the client.
To modify the response headers and response data if needed.
The filters can be used for filtering request, user authentication, data compression, data
encryption, image conversion, logging and auditing etc.
After creating a filter you need to deploy it in deployment descriptor file web.xml and then
map that filter to servlet name or url pattern.You can use one or more filter for one servlet.
So whenever servlet will be called first request will go to filter and then to the servlet.
A filter implements javax.servlet.Filter . A Filter class has three abstract methods which
needs to be implement :
1) void init() : The init method is used to prepare the filter for service. All the initial
configuration is set inside this method. The filter's init method called only once in the
lifetime of servlet. Syntax :
void init(FilterConfig config) throws ServletException
2) void doFilter() : This method is the main method which provides the services. The doFilter
() method called each time when the request and response is coming. Filter's doFilter() method
receives the request and response and FilterChain containing the filter which need to be run
next. In the doFilter method you can modify the request and response object, after that filter
calls the chain.doFilter() method to transfer control to the next filter. Syntax :
void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
3) void destroy() : This method is called by the web container to indicate that the filter is
being taken out of service. Syntex :
void destroy()
For Example :
package com.evon.filter.example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TrackTimeFilter implements Filter {
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long beforeRunServlet = System.currentTimeMillis();
chain.doFilter(request, response);
long afterRunServlet = System.currentTimeMillis();
String name = "";
System.out.println("Total time to run servlet is " + (after - before) + "ms");
}
}
Above we have created the filter, now to use this filter we need to register it in deployment
descriptor file web.xml. Following is the code sample of web.xml :
<filter>
<filter-name>TimerFilter</filter-name>
<filter-class>com.evon.filter.example.TrackTimeFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Paramter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>TimerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The above filter would apply to all the servlets because we specified /* in our configuration.
You can specicy a particular servlet path if you want to apply filter on few servlets only.
You can see such more blogs at http://findnerd.com/NerdDigest
Servlet filters are the classes which can intercept the request and can modify the response.
Filters are not servlets, because filters can not create the response.The purpose of using
filters are as follows :
To intercept requests coming from a client before process it at back end.
To modify the request headers and request data if needed.
To manipulate responses coming from server before they are sent back to the client.
To modify the response headers and response data if needed.
The filters can be used for filtering request, user authentication, data compression, data
encryption, image conversion, logging and auditing etc.
After creating a filter you need to deploy it in deployment descriptor file web.xml and then
map that filter to servlet name or url pattern.You can use one or more filter for one servlet.
So whenever servlet will be called first request will go to filter and then to the servlet.
A filter implements javax.servlet.Filter . A Filter class has three abstract methods which
needs to be implement :
1) void init() : The init method is used to prepare the filter for service. All the initial
configuration is set inside this method. The filter's init method called only once in the
lifetime of servlet. Syntax :
void init(FilterConfig config) throws ServletException
2) void doFilter() : This method is the main method which provides the services. The doFilter
() method called each time when the request and response is coming. Filter's doFilter() method
receives the request and response and FilterChain containing the filter which need to be run
next. In the doFilter method you can modify the request and response object, after that filter
calls the chain.doFilter() method to transfer control to the next filter. Syntax :
void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
3) void destroy() : This method is called by the web container to indicate that the filter is
being taken out of service. Syntex :
void destroy()
For Example :
package com.evon.filter.example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TrackTimeFilter implements Filter {
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
config = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
long beforeRunServlet = System.currentTimeMillis();
chain.doFilter(request, response);
long afterRunServlet = System.currentTimeMillis();
String name = "";
System.out.println("Total time to run servlet is " + (after - before) + "ms");
}
}
Above we have created the filter, now to use this filter we need to register it in deployment
descriptor file web.xml. Following is the code sample of web.xml :
<filter>
<filter-name>TimerFilter</filter-name>
<filter-class>com.evon.filter.example.TrackTimeFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Initialization Paramter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>TimerFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The above filter would apply to all the servlets because we specified /* in our configuration.
You can specicy a particular servlet path if you want to apply filter on few servlets only.
You can see such more blogs at http://findnerd.com/NerdDigest
No comments:
Post a Comment