Definititon of Filters:
Filters are Java components-very similar to servlets - that you can use to intercept and process request before they are sent to the servlet, or to process response after the servlet has completed,but before the
response goes back to the client.
Suppose if you want to do some thing before doGet/doPost is invoked (like auditing related activity(How many requests came to servlet etc)) you can write Filter.We have to map which filters will be called for which request URL pattersns in DD(web.xml)
Filter is an interface just like Servlet.
What happens in Background ?
As you know container passes request,response objects to doGet/doPost.If a filter is configured, container
invokes the Filter and passes the request and response objects to Filter and then request get passed to
Servlet(If no other Filters are configured.We can configure more than one filter for a request.Eg:AuditingFilter may just log the request details to HelloWorldServlet,SecurityFilter performs security checks for the same HelloWorldServlet).
Suppose if there are more Filters for Servlet, container invokes Filter1 and then Filter7 and Filter3.
as in the following image.But Servlet even doesn't know that Filters are configured.

Filter methods:
init(Just like Servlet)
destroy(Just like Servlet)
doFilter:
The doFilter() method is called every time the Container determines that the filter should be
applied to the current request.The doFilter() method takes 3 arguments.
a)ServletRequest
b)ServletResponse
c) FilterChain
Auditing related/Security related code you like to implement should be in doFilter.
How to declare Filter ?
------------------------------
From the above declaration you can see it is very similar to Servlet mapping.
Whenever a request ends with .do,then BeerRequestFilter gets executed.
When there are 3 filters configured for HelloWorldServlet,container first invokes
Filter1, In Filter1's doFilter method you have to call chain.doFilter(req,resp),then
Filter2 gets executed,Filter2 has to write chain.doFilter then Filter3 gets executed
and as it is the final Filter then request goes to HelloWorldServlet,then response
comes to Filter3 you can do any post processing logic,then Filter2 then Filter1
and response comes to browser
Request->Filter1->Filter2->Filter3->HelloWorldServlet->Response
->Filter3->Filter2->Filter1->Browser.
ImpThing:
Don't forget to add chain.doFilter in every filter.....
Uploaded the zip file with Simple Filter example
https://docs.google.com/file/d/0BwZaaDwCofcNcTdqcTlCQnZwakk/edit?usp=sharing
Please let me know if you can't access the above URL.
Filters are Java components-very similar to servlets - that you can use to intercept and process request before they are sent to the servlet, or to process response after the servlet has completed,but before the
response goes back to the client.
Suppose if you want to do some thing before doGet/doPost is invoked (like auditing related activity(How many requests came to servlet etc)) you can write Filter.We have to map which filters will be called for which request URL pattersns in DD(web.xml)
Filter is an interface just like Servlet.
What happens in Background ?
As you know container passes request,response objects to doGet/doPost.If a filter is configured, container
invokes the Filter and passes the request and response objects to Filter and then request get passed to
Servlet(If no other Filters are configured.We can configure more than one filter for a request.Eg:AuditingFilter may just log the request details to HelloWorldServlet,SecurityFilter performs security checks for the same HelloWorldServlet).
Suppose if there are more Filters for Servlet, container invokes Filter1 and then Filter7 and Filter3.
as in the following image.But Servlet even doesn't know that Filters are configured.
Filter methods:
init(Just like Servlet)
destroy(Just like Servlet)
doFilter:
The doFilter() method is called every time the Container determines that the filter should be
applied to the current request.The doFilter() method takes 3 arguments.
a)ServletRequest
b)ServletResponse
c) FilterChain
Auditing related/Security related code you like to implement should be in doFilter.
How to declare Filter ?
------------------------------
From the above declaration you can see it is very similar to Servlet mapping.
Whenever a request ends with .do,then BeerRequestFilter gets executed.
When there are 3 filters configured for HelloWorldServlet,container first invokes
Filter1, In Filter1's doFilter method you have to call chain.doFilter(req,resp),then
Filter2 gets executed,Filter2 has to write chain.doFilter then Filter3 gets executed
and as it is the final Filter then request goes to HelloWorldServlet,then response
comes to Filter3 you can do any post processing logic,then Filter2 then Filter1
and response comes to browser
Request->Filter1->Filter2->Filter3->HelloWorldServlet->Response
->Filter3->Filter2->Filter1->Browser.
ImpThing:
Don't forget to add chain.doFilter in every filter.....
Uploaded the zip file with Simple Filter example
https://docs.google.com/file/d/0BwZaaDwCofcNcTdqcTlCQnZwakk/edit?usp=sharing
Please let me know if you can't access the above URL.
No comments:
Post a Comment