wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes When to use HttpRequestWrapper? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "When to use HttpRequestWrapper?" Watch "When to use HttpRequestWrapper?" New topic
Author

When to use HttpRequestWrapper?

Salvador Cecilio
Ranch Hand

Joined: Dec 20, 2004
Posts: 41
When would it be appropriate to use a HttpRequestWrapper?


SCJP 1.4 - 93%
SCWCD 1.4 - 89%
IBM FileNet 3.5 - Developer
IBM FileNet 3.5 - Administrator
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

My favorite example is in using a wrapper to trim all of the request parameter values. It's easy for a user to accidently hit the space bar at the end of an entered number. This wrapper saves a lot of frustration!


A good workman is known by his tools.
Salvador Cecilio
Ranch Hand

Joined: Dec 20, 2004
Posts: 41
Hmmm didn't know you could modify parameters.

I'm looking at the API of the HttpRequestWrapper and would use the getParameter() method to access the parameter.

The question I have is how do you modify the parameter values when data has been submitted via querystring or posted.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Just override the getParameter() method like this:
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

There's a couple other methods that retrieve parameters that need to be covered as well, but that's the jist of it. I also like to hold a lazy map of the parameters in my wrapper so they only get trimmed once (and actually never get trimmed if you never ask for any parameters).
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Originally posted by Salvador Cecilio:
Hmmm didn't know you could modify parameters.

I'm looking at the API of the HttpRequestWrapper and would use the getParameter() method to access the parameter.

The question I have is how do you modify the parameter values when data has been submitted via querystring or posted.


As Paul shows, you don't actually change the parameters but by wrapping the request you make your application see the kind of values you want it to see. When the app asks the request for a parameter, it doesn't know that the request is wrapped and giving it a special, new-and-improved version of the original parameter.

Wrapping the request is like gold-plating costume jewelry. You're not changing the original product (only coating it), yet you fool everyone into thinking the product is better than it really is!
Salvador Cecilio
Ranch Hand

Joined: Dec 20, 2004
Posts: 41
Originally posted by Marc Peabody:


As Paul shows, you don't actually change the parameters but by wrapping the request you make your application see the kind of values you want it to see. When the app asks the request for a parameter, it doesn't know that the request is wrapped and giving it a special, new-and-improved version of the original parameter.

Wrapping the request is like gold-plating costume jewelry. You're not changing the original product (only coating it), yet you fool everyone into thinking the product is better than it really is!



Yeah, that makes a lot more sense, and I thought I knew my decorator patterns.
 
GeeCON Prague 2014
 
subject: When to use HttpRequestWrapper?