This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Passing an object between two JSPs via Session-attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing an object between two JSPs via Session-attribute" Watch "Passing an object between two JSPs via Session-attribute" New topic
Author

Passing an object between two JSPs via Session-attribute

Bela Takacs
Greenhorn

Joined: Sep 24, 2012
Posts: 5
Dear JSP programmers,

My first JSP: SpecialImage.jsp



My second JSP: OneImage.jsp (Yes, I know, it is a scriptlet, but that is not the point here. At least, not at the moment.)



The results of the System.out.println:
Param ISBN: 1451581718 bi.ISBN: 0132618206
Param ISBN: 1449380344 bi.ISBN: 0132618206
Param ISBN: 0596800959 bi.ISBN: 0132618206
Param ISBN: 0470502266 bi.ISBN: 0132618206
Param ISBN: 0132618206 bi.ISBN: 0132618206

It is strange and bad, that "bi.ISBN" has always the same value (0132618206).
I think, "Param ISBN" and "bi.ISBN" should always have the same value. I mean, if in an iteration the "Param ISBN"=1451581718, then the "bi.ISBN" should also have the value of 1451581718.

Remark:
As the title of my topic suggests, my original problem is, that I need the value of the forEach variable "oneItem" in my second JSP (OneImage.jsp). So, as you see in my first JSP (SpecialImage.jsp) I try to use the core:set tag with scope=session. But I am unsuccessful, because the value of bi.ISBN is always the same (in each iteration). Please, tell me, how could I get the value of the forEach variable "oneItem" in my second JSP.

Kind regards,
Bela

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

What is the relationship between the JSPs?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Notice how the number you retrieve from the session is the last one in the list?
Think about the way that a web page loads.

It calls SpecialImage.jsp. The iteration runs. It generates a bunch of img tags and returns HTML.
The browser reads the HTML and then issues requests to load each image on the page.
So if you look at a network trace you would see

SpecialImage.jsp
OneImage.jsp?ISBN=1451581718
OneImage.jsp?ISBN=1449380344
OneImage.jsp?ISBN=0596800959
...

Using the request parameter seems to be working. Why don't you stick with that?
Bela Takacs
Greenhorn

Joined: Sep 24, 2012
Posts: 5
Dear Bear,

I don't know exactly what you mean by the word "relationship" between the two JSPs. The SpecialImage.jsp has a forEach iteration. It iterates over a java.util.List of Books. Books are POJO-s, with variables like: title, author, ISBN_number, coverImage, usw. (I select these Books from Hibernate/MySQL.) So, ${allItems} is a List of Books. In the forEach iteration, ${oneItem} represents the Book, that is investigated in the n-th iteration.

In every iteration the SpecialImage.jsp puts the ${oneItem} object into the session (variable "theGivenBook") and then SpecialImage.jsp "calls" OneImage.jsp. Have a look at the code again:



OneImage.jsp tries to get the object that SpecialImage.jsp has put into the session. So, for example, if I have got 20 Books, I mean, ${allItems} has twenty ${oneItem}, then OneImage.jsp will be called twenty times. And, of course, OneImage.jsp will try to get the variable "theGivenBook" from the session 20 times. So the code:


My problem is: the OneImage.jsp always gets the last value of variable "theGivenBook". I mean, when I receive the value of "theGivenBook" in OneImage.jsp, "theGivenBook" always has the very same value (although I set it to a different value in each and every iteration). For example, if I have 20 Books, then in the OneImage.jsp, the variable "theGivenBook" will always contain the very same Book object (regardless of iteration).

Bear, please, tell me, if I am not clear enough.

Kind regards,
Bela
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

By relationship, I meant how do you transition from one page to the other?

But Stefan has already explained your issue.
Bela Takacs
Greenhorn

Joined: Sep 24, 2012
Posts: 5
Dear Stephan,

I cannot stick with the request parameter. My OneImage.jsp needs the "theGivenBook" object of SpecialImage.jsp, because the "theGivenBook" object has variables that contain images. The request variable is only a String, so it cannot contain images (as far as I know).

Is there a better way to pass an object between the two JSPs? What is the standard way to pass and receive an object?

Kind regards,
Bela
Bela Takacs
Greenhorn

Joined: Sep 24, 2012
Posts: 5
Dear Bear,

This is the transition, because SpecialImage.jsp needs an image from OneImage.jsp:


Kind regards,
Bela
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Session is fine for a scoped variable that needs to be shared across multiple pages,

As Stefan has already pointed out, the loop in the JSP executes on the server before the HTML page is sent to the browser where the image tag triggers the second JSP. So of course, the variable has the last value that it obtained by the loop.
Bela Takacs
Greenhorn

Joined: Sep 24, 2012
Posts: 5
Dear Bear and Stefan,

Thank you very much for your help!! Based on your answers, I created a solution. I post it, because someone might be interested. So, the SpecialImage.jsp:



And the OneImage.jsp:



@Bear: Now, I am trying to make this happen without scriptlet code. I am a Greenhorn here, so I ask: if I encounter problems with getting rid of the scriptlet code, should I open a new topic, or should I post my question to this topic?

Best regards
Bela
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

New questions should be opened in new topics. If it's a continuation of this question, it should be continued here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing an object between two JSPs via Session-attribute
 
Similar Threads
Spring Web - DisplayLoading image while processing.
JSTL forEach Tag not working
JSP set and out
Problems with the Example from HF Servlets & JSP in Chapter 3
Displaying the right details of a resource taken from a JSP results page