JSP - Populating text Input fields from the query string
I am building a search application under which, a jsp calls the servlet with a certain search criteria
The design is such that, once the servlet find the appropriate matching record (from the DB), it redirects the page to the original JSP concatenating the query string with the search results.
Here, am facing the problem of populating the text boxes on the jsp with the values from the query string. The Query string formulates as expected but the jsp fails to pick up values from the query string.
The jsp code looks like :
<input type="text" name="username" value = <% request.getParameter("username"); %> width="75" />
There are 3 more such text inputs.
Another prb I face is, when the query string doesn't have any thing (as in the first request to the page), the text boxes are populated with "width="75" ".
Can you someone show me wayout ?
Any help would be highly appreciated.
Thanks - JiNN
Thanks for your time dude.
But that still hasn't resolved my prb. The box gets populated with "null" when there is no query string attached. So to ruse through it, I wrote a method which is as follow :
code]
public String populateField(String str)
{
if ( str != null )
{
return str;
}
else
{
return new String(" ");
}
}[
[/code]
and the earlier line was modified as :
code]
<input type="text" name="phone" value= <%= request.getParameter("phone") %> width="75" />[
[/code]
Now, the field gets populated wtih " width="75" "
Can you pls show me the light ?
Yes, I had an error in my earlier post. What I mean is
<input type="text" name= "username" value = "<% =request.getParameter("username") %>" width="75" />
Note the quotation marks that I forgot before.
Hope this helps.
Niklas
Ok, there are several ways to solve this:
a) Dispatch through a servlet which copies the parameter to an attribute, set it to empty string if null, and use request.getAttribute in your input tag.
or
b) Set the variable in a scriptlet at an earlier place in the page:
<% String var=request.getParameter("username");
if (var==null) var="";
%>
<input type="text" name= "username" value = "<% =var %>" width="75" />
or
c) Use javascript to set the field according to spec
Good luck
Niklas