• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing Variables

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Mike London
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent. You now have a powerful new tool in your virtual toolbox
 
reply
    Bookmark Topic Watch Topic
  • New Topic