Removing whitespace from post to servlet.

Hello,

My web application involves posting html forms to a servlet, and storing these values in a database.

Sometimes the user enters extra white before there input text in the html form, to avoid these white spaces being stored in the database. Should we normally try to remove these extra white spacing using javascript before the form is submitted.

Or use the String trim() function in the servlet to remove any additional spacing.

Thanks

[472 byte] By [sam04a] at [2007-10-2 13:50:20]
# 1
I would go with trimming in the servlet.The user might have javascript disabled, or might bypass it completely. Handling all validation/trimming on the server side is guarunteed to be run, where the javascript is not.
evnafetsa at 2007-7-13 11:50:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Its actually good to do validation on both sides - do it in javascript on the client side and then do it again on the server side.

If javascript is turned off on the client, you will catch the errors on the server side. But if its turned on, you will save just a little bit of processing time on the server :)

somaiaha at 2007-7-13 11:50:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

why not take it into an intermediate servlet or jsp and carrying out an URL encoding method for doing it... I'm sure that'll make the data safe... whatever it might be.... whitespace or else...

Make sure u get the data to be encoded thru the doGet method in that intermediate servlet. Also while passing it to that servlet, u have to give GET specification for the form's method attribute.

Hope that helps.

nagraj.raoa at 2007-7-13 11:50:51 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...