Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Servlets
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Servlets
How to use Filter in Servlets?
velayutham rajaganapathi
Greenhorn
Posts: 1
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Example Program
package filters; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.util.Date; public class TimerFilter implements Filter { private FilterConfig _filterConfig = null; /** * The server calls this method to initialize the Filter and * passes in a FilterConfig object. */ public void init (FilterConfig filterConfig) throws javax.servlet.ServletException { _filterConfig = filterConfig; } /** * Return the FilterConfig object */ public FilterConfig getFilterConfig() { return _filterConfig; } /** * EAServer calls this method each time a servlet, JSP or static Web * resource is invoked. */ public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, javax.servlet.ServletException { // This is executed before the servlet/jsp/static resource is served. long startTime = System.currentTimeMillis(); // Pass control to the next filter in the chain. chain.doFilter(request, response); // This is executed after the servlet/jsp/static resource has been served. long endTime = System.currentTimeMillis(); // Get the ServletContext from the FilterConfig ServletContext context = _filterConfig.getServletContext(); // Get the type parameter from the filter's initialization // paramters. Return null if the parameter was not set String type = (String)_filterConfig.getInitParameter("type"); // Get the filter’s name to include in the log String filterName = _filterConfig.getFilterName(); HttpServletRequest httprequest = (HttpServletRequest)request; String path = httprequest.getRequestURI(); // By default, record the absolute time if ((type == null) || (type.equals("absolute"))) { Date date = new Date(endTime); context.log(filterName + " - " + path + " finished: " + date.toString()); } else { context.log(filterName + " - time to process " + path + ": " + (endTime - startTime) + "ms"); } } /** * Notifies the filter that it is being taken out of service. */ public void destroy() { // free resources } }
Paul Sturrock
Bartender
Posts: 10336
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
So what is your question exactly?
JavaRanch FAQ
HowToAskQuestionsOnJavaRanch
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Using Filters with Struts
Query on Request Dispatcher.
Tomcat servlet-mapping bypass if real file requested
How to implement Request Time Out
Session Advice
More...