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]

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>
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