New to JSP's and fighting with request.getParameter()

I have built the simple jsp below and am trying to pass it two variables through a HTML form with GET action. The code looks right according to the tutorials and research I have done but it still does not work. I am running Tomcat 5.5 with jdk1.5.0_05. Please take a look and let me know if I am either making a rookie mistake or if I may have a config issue.

Thanks.

.<HTML>

<HEAD>

<title>Untitled Document</title>

</HEAD>

<BODY bgcolor="#666666">

<% if (request.getParameter("username") == null && request.getParameter("password") == null) { %>

<FORM ACTION="index.jsp" METHOD="GET">

<P>

User Name: <input type="text" name= "username" size=26>

<P>

Password: <input type="text" name= "password" size=26>

<P>

<input type="submit" value="Submit">

</FORM>

<% } else { %>

<%! String username, password; %>

<%

username = request.getParameter("username");

password = request.getParameter("password");

%>

<P>

<B>You have provided the following info</B>:

<P>

<B>Name</B>: <%= username %><P>

<B>Email</B>: <%= password %>

<% } %>

</BODY>

</HTML>

[1419 byte] By [JHamiltona] at [2007-10-2 0:59:11]
# 1
I do not know if this is the problem, but I think you shoudl try declaring your strings at the top of the page NOT inside an if statement.
angrycata at 2007-7-15 18:19:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Works for me, dont know if that was the problem or not, but I just tried it and it works ok, note the file was named Untitled-1.jsp when i ran it.

<HTML> <HEAD> <title>Untitled Document</title> </HEAD> <BODY bgcolor="#666666">

<%

String username;

String password;

if (request.getParameter("username") == null && request.getParameter("password") == null)

{

%> <FORM ACTION="Untitled-1.jsp" METHOD="GET">

User Name: <input type="text" name= "username" size=26>

Password: <input type="text" name= "password" size=26>

<input type="submit" value="Submit"> </FORM>

<%

}

else

{

username = request.getParameter("username");

password = request.getParameter("password");

%>

<B>You have provided the following info</B>:

<B>Name</B>: <%= username %>

<B>Email</B>: <%= password %>

<%

}

%>

</BODY>

</HTML>

angrycata at 2007-7-15 18:19:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I think the problem is with form closing.try this, close the form just before body tag,beco'z u r clsing the form at the end if only,else part validation also should come under form.so close form at the end of else part.
arpithaa at 2007-7-15 18:19:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi JHamilton,

The jsp code provided by you is absolutely correct. The problem you are facing is with the name of the jsp file.

The "action" attribute in the <form> tag, specifies where the request is to be submitted. In your case you want the request to be submitted to the same jsp, so that you can display what user just entered. So, change the action attribute to the filename of the jsp and it should work fine.

Eg. if filename is "index.jsp", then form tag should be,

<form action="index.jsp">

--

Mohnish

mohnish82a at 2007-7-15 18:19:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
In Form tag try the following<form action=" http://localhost/index.jsp" action="GET">
sandy4javaa at 2007-7-15 18:19:26 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...