Reading parameter values in JSP files
I have a form that is presented in a JSP file, which the user then submits. This is sent to a servlet that examines the form. If it detects an error, it returns to the form page and redisplays it.
I would like to have the form automatically fill in any text fields for which the value is already known. Eg, if the form was invoked with the parameter 'username' set to 'Bob', I want to have the coresponding input field's value automatically set to 'Bob'.
The code I'm trying looks like this, but it doesn't work. Is there an easy way to get the JSP to do what I want? Also, do I have to specially handle the initial case where the username is not set?
<input type="text" name="username" value="${username}" />
Message was edited by:
Mark_McKay
# 1
is the value being passed to a next page or the same page?
well u coud use
if on the same page :
<input type="text" name="username" value='<%=username%>'>
if from previous page
<input type="text" name="username" value='<%=request.getParameter(username)%>'>
2. are u trying to reload the same page depending on whether a variable has been set or not?
you coud use a try catch...
# 2
> I have a form that is presented in a JSP file, which
> the user then submits. This is sent to a servlet
> that examines the form. If it detects an error, it
> returns to the form page and redisplays it.
>
> I would like to have the form automatically fill in
> any text fields for which the value is already known.
> Eg, if the form was invoked with the parameter
> 'username' set to 'Bob', I want to have the
> coresponding input field's value automatically set
> to 'Bob'.
>
> The code I'm trying looks like this, but it doesn't
> work. Is there an easy way to get the JSP to do what
> I want? >
> <input type="text" name="username"
> value="${username}" />
>
> Message was edited by:
> Mark_McKay
EL has an implicit object called 'params' which holds the request parameters. So if you know the name of your request parameter, you can query for it as ${params.paramName}. Thsu your code would be
<input type="text" name="username"
value="${params.username}" />
>Also, do I have to specially handle the
> initial case where the username is not set?
No, if it doesnt find a request param by that name, as is the case when the form is first submitted, then it would just blank out.
ram.