File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Wrapper class of ServletResponse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Wrapper class of ServletResponse" Watch "Wrapper class of ServletResponse" New topic
Author

Wrapper class of ServletResponse

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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.

My answer is:

HOW?

SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
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.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, thanks.
But how our invocations are propagated to the original ServletResponse object?
We have to invoke super.xxx() at the beginnig of our overriding function to do this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

No. You aren't overriding ServletResponse here so you wouldn't call super(). You are delegating to it, so you call its methods directly as in line 19 of Travis's example.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

But I extend ServletResponseWrapper and I override its methods. My overriding methods must have influance on the original ServletResponse object. How can it be done?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

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.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I do not think you understand me well.
ServletResponseWrapper passes all of the invocations to the original ServletResponse object. How can it be possible if I override its methods?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.

Hope this helps.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Lucas Smith wrote:I do not think you understand me well.
ServletResponseWrapper passes all of the invocations to the original ServletResponse object. How can it be possible if I override its methods?


This was NOT asked in the very first question. Hence it might have created a confusion I believe!
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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 .
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Lucas Smith wrote:I do not think you understand me well.
ServletResponseWrapper passes all of the invocations to the original ServletResponse object. How can it be possible if I override its methods?


To be frank and straight forward, it would NOT be possible until you invoke super.xxx().

But it is strongly advisable NOT to override such functionality until you are very sure and proper to take care of all necessary actions. Otherwise, it would behave in a haphazard manner at run time.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Really clear for me now. Thanks once again
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Lucas Smith wrote:
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 .


Exactly! You now got the point
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Lucas Smith wrote:Really clear for me now. Thanks once again


My pleasure
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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?
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
Yes, this is where you would use the ServletResponse wrapper and override the response.getOutputStream() method to work with with a ServletOutputStream wrapper .

See the CompressionResWrapper in the http://javawebparts.sourceforge.net
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks Travis.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper class of ServletResponse