First, I'm going to offer some unsolicited advice:) Even if your javascript code trims the input from the user, you cannot guarantee that this code will always run. For example, a user might disable javascript on their browser or just use a library (lots of them exist) to programmatically submit form data without using any browser.
So, you should always trim the input on the server side (in your case, in the JSP). In production systems, you need to do a little more than simply trimming, you need to check maximum sizes and data types as well (so if your JSP requires a numeric input, you need to check for that)
What this means is that you essentially have to do data validation twice. The data validation that you do in the browser is there to help the user and it should show a user friendly message when something invalid is entered. The important concept is that the browser validation is not there to provide security as it doesn't provide any. The security comes from the server side validation. It's not so important for server side validation to provide user friendly feedback; it's nice if it does but it's not the top priority.
Now, to your question. When you call something like
trim(document.insert.aname.value)
this doesn't change the value in the form. It just gives back a trimmed version of the form data. So when you submit the form, the space at the end of 'test' goes along with the form data.
It's possible for javascript to manipulate what is in the form but this is not commonly done and is not advisable. So, to summarise:
- Your code does not remove the trailing space for the user submitted data
- You could change you code to do this but it wouldn't be a good idea as you will have to perform the trim on the server regardless
- Therefore, you do need to trim it on the server
Hope this helps.
Phil