• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper class of ServletResponse

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Really clear for me now. Thanks once again
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



My pleasure
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Travis.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic