• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hidden Fields in JSP

 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have three JSP pages namely one.jsp,two.jsp, and three.jsp.In one.jsp i have username and password text field.I print the username in two.jsp using
<input type="text" name="username">
<input type="password" name="password">
String user = request.getParameter("username");
welcome , <%=user%>

I have two fields city and state in two.jsp
<input type="text" name="city">
<input type="text" name="state">
and a hidden field,
<input type="hidden" name="uname">

now, how can i print the same username in three.jsp also using hiddenfeilds.
Can anyone tell this for me.
how should i get the same username in the three.jsp
Thanks in advance.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to follow the way you are doing things,
hidden field to two.jsp will look like this:

and three.jsp will read it the same way two.jsp is right now:

you might cause confusion by calling the field on two.jsp by "uname"


An alternative is to add it to the session.
 
Chetan Chauhan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code for one.jsp
---------------------
<input type="text" name="username">
<input type="password" name="password">

You wrote code for two.jsp
----------------------------
String user = request.getParameter("username");
welcome , <%=user%>

U have two fields city and state in two.jsp
<input type="text" name="city">
<input type="text" name="state">
and a hidden field,
<input type="hidden" name="uname">

in two.jsp set the value for "uname" hidden parameter equals to "username" which you received as request

Then Code for three.jsp should be
----------------------------------
String user = request.getParameter("uname");
welcome , <%=user%> on Page Three
 
Shilpa Tendulkar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to persist the value of "username" in your application, I would suggest you to use session.
1) When you get the value of username first time, set it to session like:
String username = reqeust.getParameter("username");
HttpSession userSession = request.getSession();
userSession.setAttribute("userName",userName);
2) in other pages wherever you want to access it use:
String uname = (String)session.getAttribute("userName");
out.println("uname "+uname);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!