aspose file tools*
The moose likes JSP and the fly likes Passing Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing Variables" Watch "Passing Variables" New topic
Author

Passing Variables

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
How do you pass variables from one JSP page to another when you're not using a SUBMIT?
For example, I have three JSP pages.
Page 1 gets input from the user and calls Page 2.
Page 2 does a database lookup (using a bean) and presents the results as links.
What I want is that when I click on one of the links on page 2 and go to page 3, I can "pass a parameter" from page 2 to page 3 and still be able to use the variables from page 2 in page 3 to do more stuff with another bean.
I tried using <jsp:forward> but that skipped page 2 and went directly to page 3 (though it *did* have the correct variables passed).
I also tried the <FORM ACTION="URL for page3">, but that didn't include the variables from page 2.
I'm sure this is (probably) easy, but I don't see how to pass a variable from one JSP page to the next when a "SUBMIT" is *not* involved.
I looked and re-looked though the "COre/More" books, but could see an answer to this question.
Thanks for anyone's input.
-- Mike
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
If I'm not mistaken what is missing from your description is the Session object.
Any information you retrieve from the database in page 2, can be placed in the Session (say in an ArrayList). Then you just need to build a URL of the form:
page3.jsp?id=4
then in page 3 read the "id" parameter, convert it to an int, and look up the details from the stored List.
Does this make sense, or have I misunderstood the problem ?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
What I need to do is, using the earlier idea, is to pass the variable to the DBAccess_3.jsp. However, with the syntax below (from page 2 JSP), the "ttableName" gets passed as a literal (string) not as a variable.
---------------------------
out.println("<p><L1>Table Name: <A HREF='http://localhost:8081/DBAcess_3.jsp?tableName=ttableName'/>" + objectArray[i] + "</A>") ;
-----------------------------
I have the table name passed to page 2 via the POST and I can create the ttablename variable in page 2, but...
Is there a way to pass the ttablename variable, not the character string literal in the http line above?
Thanks much for your earlier reply.
I look forward to hearing back.
-- Mike
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I don't want to bang on about it, but the answer is to put the variable in the Session. You can't pass anything more than simple text in the parameters of an HTTP request.
The Servlet and JSP APIs provide three levels of Context: Request, Session and Application. Each context is basically a Map of names to values. You can put any objects you like in there, with any names you choose.
The Request context only persists for oe request, so it's only really useful for passing variables to "include"d pages.
The Session context persists for one user/browser session, or until it times out or is explicitly terminated. This is a good choice for most "in progress" data.
The Application context lasts as long as the web application is running, and is shared between all requests. It's best left for essentially static information.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
Thanks much.
I am new to JSP and didn't understand the session explanation.
I'll go and look it up.
Thanks again.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1083
I used:
<%session.setAttribute( "theTableName", tableName );%>
To set a variable in the session on page 2.
And...
<%= session.getAttribute( "theTableName" ) %>
To retrieve the variable on page 3.
This works fine.
Thanks again.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Excellent. You now have a powerful new tool in your virtual toolbox
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Variables