Top Banner
Introduction 1. Filters help in writing logic that applies to an entire web application. 2. So all such logic is written here. 3. Filters are like java components that can a. intercept and process requests before they are sent to servlet b. process response after the servlet has completed and before it is sent back to client 4. the container decides when to invoke the filter based on declaratios in DD 5. in the DD the deployer configures which filter wud be invoked for which request url patterns.
14
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 13

Introduction

1. Filters help in writing logic that applies to an entire web application.2. So all such logic is written here.3. Filters are like java components that can

a. intercept and process requests before they are sent to servletb. process response after the servlet has completed and before it is sent back to client

4. the container decides when to invoke the filter based on declaratios in DD5. in the DD the deployer configures which filter wud be invoked for which request url patterns.

6. Chained : filters ca be chained together to run one after another.7. Filters are designed to be totally self contained.

Page 2: 13

8. 3 ways filters are like servletsa. Container knows their API

i. When a class implements the Filter interface its no longer a POJO it becomes a Filter

ii. Other members of this API allow filter access to ServletContext and to be linked to oine another

b. Container manages their lifecycle : init(),doFilter(),destroy()c. They’re declared in the DD.

Page 3: 13

9. Filter’s Life Cycle : every filter must implement the 3 methods.a. init() : set-up tasks.Most common is saving a reference to FilterConfig object to be used

later.b. doFilter() :

i. called every time the container decides that filter has to be applied to this request.

ii. This method implements ur filter’s function : log user names,compress response,etc.

iii. It takes 3 arguments : ServletRequest,ServetResponse,FilterChainc. destroy() : when the conainer decides to remove a filter.any clean up task is done here.

10. FilterChain knows what comes next based on elements u specify in DD

Page 4: 13

11. chain.doFilter() calls the next filter or the Servlet’s or JSP’s service() if it’s the end of chain.

Declaring and Ordering Filters

As of version 2.4 filters can be applied to RequestDispatchers

Page 5: 13

Response Filter

1. Bu the problem is before the doFilter of the 1st filter tries to compress the servlet might send the output to the client.

2. So we need to control the output.

Page 6: 13

3. The solution : instead of passing the real response object pass a custom response object whose output stream u control

4. Implement our own responsea. Create our custom implementation of HttpServetResponseb. This implementation must also include a custom output stream as well , since that’s the

goal – to capture the output after the servlet writes to it but before it is set back to client.

Page 7: 13

5. Problem : now we have to implement all the methods I the HttpServletResponse interface

Page 8: 13

6. Solution : Wrapper classesa. They implement all the methods for the class u r trying to wrap delegating all calls to the

underlying request or response objectb. All u need to do is extend the wrapper and override the methods u need to do ur

custom work

Page 9: 13

c. These classes r a little different the the Listener adapter classes for GUI’s , they don’t just provide an interface implementation they hold a reference to the object of the same interface type to which they delegate method calls.

d. Wrapper classes created by suni. ServletRequestWrapper

ii. HttpServletRequestWrapperiii. ServletResponseWrapperiv. HttpServletResponseWrapper

Page 10: 13

A full example

Page 11: 13
Page 12: 13
Page 13: 13

Wrappers implement the Decorator pattern