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

[1074 byte] By [Jinnovatora] at [2007-10-2 20:41:45]
# 1
Hello,try the following instead:<input type="text" name= "username" value = <% =request.getParameter("username") %> width="75" />
Niklasa at 2007-7-13 23:25:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

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 ?

Jinnovatora at 2007-7-13 23:25:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

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

Niklasa at 2007-7-13 23:25:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Still shows up "null" incase of no query strings attached ! :(
Jinnovatora at 2007-7-13 23:25:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

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

Niklasa at 2007-7-13 23:25:04 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...