Calvin
Calvin

Reputation:

WhiteSpace Form TextBoxes

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
<td align = "right"><font color = "#FF0000">*</font><font face= "Calibri, Arial" size = "3"> Policy #:</font></td>
<td><input type="text" name= "policyNum" size="19" tabindex = "1" value="
<% out.println(session.getAttribute("policyNum");%>">

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.

Upvotes: 0

Views: 247

Answers (4)

Calvin
Calvin

Reputation:

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.

Upvotes: 0

Damo
Damo

Reputation: 11550

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"));

Upvotes: 0

Clint
Clint

Reputation: 9058

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.

Upvotes: 1

Dustin Kendall
Dustin Kendall

Reputation: 520

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."

Upvotes: 0

Related Questions