What are Servlet Filters ?<br /><ul><li>  Discuss about customization of request and response using filters</li></ul>Filte...
Requestsbefore the servlet was processed
Response immediately after the request was processed or before the </li></ul>    response sent to the client<br /><ul><li>...
  Used to perform various functions across the multiple servlets in a Web </li></ul>      application <br /><ul><li>  To a...
  Also create reusable functions to use in multiple Web applications</li></li></ul><li>What are Servlet Filters ?<br />Exa...
  Filter intercepts the request - check whether the sufficient permission was </li></ul>      given to the user to access ...
  User actions
  Compress the output streams
  Very flexible because we won’t write the filters within servlets
  Configured in the web.xml</li></li></ul><li>What are Servlet Filters ?<br />Implementation of Filter<br />Request<br />R...
Upcoming SlideShare
Loading in …5
×

Servlet Filters

1,346 views

Published on

Published in: Education, Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,346
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
61
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Servlet Filters

  1. 1. What are Servlet Filters ?<br /><ul><li> Discuss about customization of request and response using filters</li></ul>Filters are used to process the content in the request or response objects <br /> of Servlets and generally they do not produce the responses. <br /><ul><li> Filters interrupt
  2. 2. Requestsbefore the servlet was processed
  3. 3. Response immediately after the request was processed or before the </li></ul> response sent to the client<br /><ul><li> Enhances the lifecycle process
  4. 4. Used to perform various functions across the multiple servlets in a Web </li></ul> application <br /><ul><li> To add features to existing servlet code
  5. 5. Also create reusable functions to use in multiple Web applications</li></li></ul><li>What are Servlet Filters ?<br />Example <br /><ul><li> Customize the authentication process using filter
  6. 6. Filter intercepts the request - check whether the sufficient permission was </li></ul> given to the user to access the requested servlet <br />Prevent the unauthorized user from accessing the servlet <br /><ul><li> Used to record
  7. 7. User actions
  8. 8. Compress the output streams
  9. 9. Very flexible because we won’t write the filters within servlets
  10. 10. Configured in the web.xml</li></li></ul><li>What are Servlet Filters ?<br />Implementation of Filter<br />Request<br />Request<br />Response<br />Response<br />Client<br />SampleFilter.java<br />Sample Servlet.java<br />Filter<br />Servlet<br />
  11. 11. What are Servlet Filters ?<br /><ul><li> To write a filter, we have to implement the Filter interface  </li></ul>“Filter” Interface<br /><ul><li> Contains three methods
  12. 12. init( )
  13. 13. doFilter( )
  14. 14. destroy( )</li></ul>init()<br /><ul><li> Called by the web container to indicate the filter that, it is available for service
  15. 15. Can’t use the filter before this method was executed </li></li></ul><li>What are Servlet Filters ?<br /><ul><li> Structure of the init method</li></ul> public void init(FilterConfigfilterConfig)<br /> throws javax.servlet.ServletException<br />{<br /> //write your codes<br />}<br />doFilter( )<br /><ul><li> Heart of the filter
  16. 16. Write the processes for filtering </li></ul>Used to provide any configuration parameters<br />
  17. 17. What are Servlet Filters ?<br /><ul><li> Method has three parameters
  18. 18. Request
  19. 19. Response
  20. 20. Object of FilterChain
  21. 21. Also called by the web container
  22. 22. Does all the filter processes
  23. 23. Calls filterChain.doFilter( ) to check any other filters are available to process </li></ul> this request <br /><ul><li> If any, it will pass the request and response object to that filter
  24. 24. Otherwise it will do post processing - filtering the response</li></ul> <br />
  25. 25. What are Servlet Filters ?<br /><ul><li> Signature of the method</li></ul>public void doFilter(ServletRequestrequest, ServletResponseresponse, <br />FilterChainchain) throws IOException, ServletException<br />{<br /> //write the codes to process the request & response<br />}<br />destroy( )<br /><ul><li> Used to remove the filter from the service
  26. 26. General Form</li></ul>public void destroy( ) <br />{<br />//write your codes<br />}<br /> <br />
  27. 27. What are Servlet Filters ?<br />Filter Chaining<br />Servlet<br />Filter-1<br />Client<br />Filter-2<br />Filter-3<br />Request<br />Response<br /><ul><li> Also use more than one filter for customizing a particular servlet</li></li></ul><li>What are Servlet Filters ?<br />F3<br />F1<br />S1<br />F2<br />S2<br />S3<br />S1, S2, S3 - Servlets<br />F1, F2, F3 - Servlet Filters<br />
  28. 28. What are Servlet Filters ?<br />doFIlter( req, res, chain)<br />{<br />chain.doFilter(req, res)<br />}<br />To process the request or to filter the request<br />Codes to filter the request<br />To intercept the servlet output or to filter the response<br />Codes to filter the response<br />

×