I know that a design pattern is used to implement that wrapper class - Decorator.
The wrapper class (ServletResponseWrapper) implements ServletResponse interface and holds a reference to the original ServletResponse object. We can override some methods of ServletResponseWrapper but
it passes all of the invocations to the original ServletResponse object.
The ServletResponseWrapper contains a reference to the original response that has been wrapped. something like this is probably going on inside it.
so in our subclass that extends ServletResponseWrapper, any methods we would override should likely not call the super.xxx(). and iif we wanted to get a hold of the wrapped response, there is the getResponse() method that we may invoke in ServletResponseWrapper to get this.
Error: Keyboard not attached. Press F1 to continue.
Yes. The original ServletResponse object is passed as a parameter to the constructor of your wrapper class (line 9) and assigned to an instance variable of the class (line 13).
Now you say you want to have influence on the original ServletResponse object? Well, you now have a reference to that object in your class (line 2). So you can call methods of that object (line 19). It's very simple, this delegation thing.
It is pretty simple. It would be like instead of sending money by keeping it directly in your pocket, you keep it in a Wallet which is again in your pocket.
So, anytime you say/refer to the money, it is the same money but not directly accessible outside but it was delegated through the Wallet. Without keeping money into the Wallet, you can NOT really make use of the money. Also, whatever operation you claim to do on the money, it is indeed done through the wallet which serves as a guard for it.
Likewise, as per the code snippet give above and Paul's explanations, anytime you refer to ServletResponse object it is done through the delegated HttpServletResponseWrapper object. For the Wrapper class to provide the ServletResponse, it has to be assigned/given a reference to the Response object first which is what done in the constructor of the above example. Anytime, you obtain the Response through the Wrapper, it is done through the assigned refernece which you can see in the line #19.
OK, thanks you very much.
That delegation is really simple, but I did not get it because of another reason .
I thought that I have to extend ServletResponseWrapper to create a wrapper. Now I know that I pass original ServletResponse object to ServletResponseWrapper's constructor .
I have one more question.
We use wrappers to make sure that the output stream will be buffered (no send to an user) and to implement some kind of functions after chain.doFilter(...) method.
Where this delaying of sending a response is implemented?
Joined: Jun 06, 2006
Yes, this is where you would use the ServletResponse wrapper and override the response.getOutputStream() method to work with with a ServletOutputStream wrapper .