Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Trimming request parameters using a wrapper and a filter to avoid code duplication  RSS feed

 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wanted to get a feel for what would be considered a best practice for this.
Do you do it in the Servlet, in the bean itself, through a filter?
Any other ideas?

Thanks
Sandeep
[ October 25, 2004: Message edited by: Sonny Gill ]
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both on the client(using JavaScript) and on the server(in the service).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any need or benefit to trim on the client unless that's a required part of any client-side validation.

You should trim (if appropriate), as well as perform any other necessary validations, in the servlet to which the form is submitted (regardless of what may or may not happen on the client).

You could trim with a generalized filter, but I don't see how that makes your job any easier. I would not rely on a value object (bean) to perform trimming as that is invading your bean with logic to account for UI artifacts.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bosun and Bear.

Bear, the reason I bring this up, is that trimming request parameters is something of a cross-cutting concern. I need to do it for pretty much every request, for example before doing a check for required parameters.

Instead of repeating that code in every request, I was considering the possibility of defining a RequestWrapper with something like -



and let that take care of it.

And have a Filter that replaces every request with this request wrapper.

My concern is that if it is a good solution, why arent more people doing this? Am I missing something here?
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonny Gil,
According to my experience, I will trim user's entry in JavaScript only if I want to validate that value in the client side (browser).

Mostly I used trim before user's entry is wrapped into an object ( I mean here is Value Object/Data Transfer Object).. and I use org.apache.commons.lang.StringUtils to do "trim" process.

As illustration, I am using Struts Framework for building web application.
I will wrapped user's entry into a Value Object at Action class before I send that object to service classes.
At that time, I trim user's entry...

Hope this helps...
Correct me if I am wrong...
thanks
daniel
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fisher,

Yes, client side validation is always a good idea to have better response time.
What I am getting at is that trimming request parameters, whether in each servlet or controller class is duplication of code. Wouldnt it be better to remove that code from all those places, and keep it in a servlet filter, provided that we know for sure that for all values submitted by the user, they have to be trimmed.

So we have a request wrapper class -


And we set a filter -



and it is all done.
[ October 25, 2004: Message edited by: Sonny Gill ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!