Click to See Complete Forum and Search --> : Jsp question: redirect to itself???


yiannakop
October 3rd, 2004, 10:15 AM
Hi everyone. I am building a jsp file that uses a simple form like:
(the file is named test.jsp)


....
<form method= "post" action = "test.jsp">
<input type=text name=username/>
<input type=submit name=operation value="Login">
</form>
....


and I want, after pressing the "submit" button the page to be directed to the same file, but this time not displaying the form, but only a message like:
"Your username is ...."

Plese give me an example on how to achieve this.
Thanx

quinman
November 10th, 2004, 05:17 AM
The simplest way would be to check the value of the form field when the page is loaded. If the field "username" is NULL or empty then display the form otherwise display the form input.

In ASP i would do it like this (you'll need to convert for JSP)

testValue=request.form("username")






if testValue="" thenresponse.write "<form method= "post" action = "test.jsp">"



response.write "<input type=text name=username>"
response.write "<input type=submit name=operation value="Login">"
response.write "</form>"



elseresponse.write "Your username is " & testValue






end if




If you had several options for the output of your page it might be best to have a hidden field in your form such as
<INPUT TYPE="HIDDEN" name="CMD" value="[val]">

Each diferent form on the page would have a different value for CMD
You can then process various options depending on the incoming value of the CMD field