tags:

views:

106

answers:

4

Hi,

I am repopulating a form field that a user previously filled out with Sessions in HTTP. I am grabbing the fields from the form through a servlet:

//On servlet String polyNum = request.getParameter("policyNum") session.setAttribute("policyNum", polyNum);

//On JSP * Policy #: ">

The Problem: When I run my JSP page, I get a leading whitespace in the textboxes of the form. Therefore, anytime I submit the form, whitespace is inserted into the database as well.

Any ideas? Any help would be greatly appreciated.

A: 

Take a look at the .trim() method in the String class:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#trim()

"Returns a copy of the string, with leading and trailing whitespace omitted."

ZIP Code Database
i have tried the trim method, but to no success. If the whitespace is in the textbox of the form, then would i need to use the trim method within the jsp?
Glad you found the answer. I'm curious as to what they looked like before and after. Would you be able to post that for completeness?
ZIP Code Database
+1  A: 

You can trim it like this:

String polyNum = request.getParameter("policyNum");
polyNum = polyNum == null ? null : polyNum.trim();
session.setAttribute("policyNum", polyNum  );

and if your jsp looks like

<input type ="text" value="
<%= session.getAttribute( "policyNum" ); %>
"/>

you will get whitespace.

Also you don't show where you are inputting into the database.

Clint
A: 

The Apache Commons StringUtils library is good for trimming as it handles nulls.

So

String polyNum = request.getParameter("policyNum");
polyNum = polyNum == null ? null : polyNum.trim();

Can become

String polyNum = StingUtils.stripToEmpty(request.getParameter("policyNum"));
Damo
A: 

It turned out that my scriplets within my jsp pages were placed syntanically wrong. I didn't need to trim whitespace after all. My forms are working fine now.

Your solution about the Apache library is interesting Damo. I appreciate your feedback.