File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing values between pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing values between pages" Watch "Passing values between pages" New topic
Author

Passing values between pages

mrsmithq
Greenhorn

Joined: Aug 17, 2001
Posts: 8
How can I pass values between pages? For example...
If I have a hidden variable and I click on a link how can I get that hidden variable value to be passed.
Joey Duguay
Greenhorn

Joined: Aug 27, 2001
Posts: 11
mrsmithq, There are a couple of ways to pass a variable from 1 page to another. Here are 3 ways that I know of.
- QueryStrings
You would change your link so that the hidden variable is in the link. This is only good if you don't care if people see the value. If you want the value to remain invisible don't select this.
- Forms
This requires a little javascript that submits a form when you click on the link. This means that all values of the fields in this form are passed to the next page. So your hidden field gets sent to the other page.
- Session Variables
This stores the information in a variable for the duration of your WWW session. It can then be accessed from the next page as well as others.
Let me know which you would like to know more about and i can paste some code examples.
Joey
mrsmithq
Greenhorn

Joined: Aug 17, 2001
Posts: 8
Well, here is what I am trying to set a variable to: <jetspeed:info requestedInfo="Screen" />
I would say session, but either option is fine.
Thanks
Joey Duguay
Greenhorn

Joined: Aug 27, 2001
Posts: 11
This is how you create a set a variable in your session.
HttpSession aServletSession = request.getSession(true);
// timeout for the session. Default is 30 minutes i beleive
aServletSession.setMaxInactiveInterval(timeout); (optional)
// Se the variable
aServletSession.setAttribute("VariableName", strValue);
Then if you wish to get the variable in another page you do the following.
// this one is false because you just want to make sure there is a session. if this returns null the user doesn't havea session.
HttpSession aServletSession = request.getSession(false);
// Get the variable's value
String value = aServletSession.getAttribute("VariableName");
I think that is all you will need. You can put different type of data in these session variables. Objects, int, String..... I used it for Vectors. So it's flexible.
Joey
mrsmithq
Greenhorn

Joined: Aug 17, 2001
Posts: 8

<jetspeed:info requestedInfo="Screen" />
This is a string but it just wot work. When I try to use the baove to set it to a hidden variable it works though...

<input type="hidden" name="current_screen"
value="<jetspeed:info requestedInfo="Screen" />">
so how can I use it to do something else?
Joey Duguay
Greenhorn

Joined: Aug 27, 2001
Posts: 11
Hello,
I'm not too familiar with JSP tags but you could use the form method. As you mentioned, you can set a hidden field to the value you wish. All you would need to do is surround the fields you wish to pass to the next field in form tags. I.e.
<FORM NAME="main" action="next.jsp">
<input type="hidden" name="current_screen" value="<jetspeed:info requestedInfo="Screen" />">
</FORM>
Then your link would look something like.
<a href='javascript:main.submit();'>
This would submit the main form and pass all the fields in this form to JSP page you have in the action. Then you just need to get the value from the querystring.
String tempValue = request.getParameter( "current_screen" );
Joey
Joey Duguay
Greenhorn

Joined: Aug 27, 2001
Posts: 11
I forgot to mention you might want to use method=POST for the form.
<FORM NAME="main" method=POST action="next.jsp">
<input type="hidden" name="current_screen" value="<jetspeed:info requestedInfo="Screen" />">
</FORM>
Joey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing values between pages
 
Similar Threads
declaring an arraylist or array type hidden variable in a form
Log4J properties in Production
Sending Values without Session
HTML Hidden value
javascript