Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ticky jsp redirect question 1

Status
Not open for further replies.

kally

Programmer
May 20, 2004
13
0
0
CA
platform details
win2k/apache2/tomcat5/mysql(not sure about version num)

goal
dynamically create a table of photo graphs which are queried from the database.
have those photographs redirect the user to a details.jsp page where information about the photo is displayed.

my approach
use a list of productbeans to store the info about the photos
use a htmlutil class to build the table of photos and make those photos into href's

why it doesn't work
I am at the same time maintaining session info. It would appear that using a hyperlink nullifies your session info.

solutions I have thought about

using formns
didn't work cuz the number of forms I need is dynamic and html is static

using redirect in the htmlutil
I can output text to the bowser, but in order to use redirect it would need to be part of a jsp which was then compiled...not a text that looks like jsp code.

passing the sessionid along in the href tag
I got the session Id to show up as a paramater on the next page, but I don't know how to turn the id string back into the session, in other words, I don't know how to fetch the session from my viewdetails class if I have the session id string

Disclaimer, I am a newb, this is my first real application and so I may be making assumptions or mistakes. I have however spent 5 hours reading forums and haven't found an awnser to this problem. Any suggested solutions, whether they resemble the original plan or not would be appreciated. Critical elements are that I have a dynamic number of product images and they need to link to a jsp page.

Regards,
Fieran Mason-Blakley
 
If you track your session id as a cookie wouldn't the problem go away and href's would work again?

if your tracking it on the URL your container should handle the transliteration for you.

I use href's and sessions all the times and have never encountered the problem you mention.
 
as I said at the bottom of my post I am a newb. I don't quite understand the interaction between the general JSP context(including context, session, response, and request).

what I think is happening is the following, when I click on the generates link...something like
<href myhost/Command?action=details&prodid=myProductID> with pics and the whole bit...that I can get to work, but when I click that link, after it has been generated by the jsp...I access the details command servlet, but I have lost the context...


I think it is because that creates a new request instead of using the old one
How do I go from this model to cookies?
 
The default mode for tomcat to handle sessions is using cookies so if your using a factory configuration it should be doing that.

Are you sure that your code is not clobbering the session on its own?

 
here is the criticl jsp code

ArrayList productsForRooms = new ArrayList();//list of product lists

ShowroomProductsXRefDAO myShowroomProductsXRefDAO = new ShowroomProductsXRefDAO();

if(showrooms.size()>0){
for(int i=0; i<showrooms.size(); i++){
productsForRooms.add(myShowroomProductsXRefDAO.getShowroomProducts((ShowroomBean)showrooms.get(i)));
}
}
System.out.println("have the list of product lists");

//generate first room
if(productsForRooms.size()>0){
System.out.println("NUMBER of PRoducts: " + ((ArrayList)productsForRooms.get(0)).size());
System.out.println(HtmlUtil.buildShowroom((ArrayList) productsForRooms.get(0),session.getId() ));
out.println(HtmlUtil.buildShowroom((ArrayList) productsForRooms.get(0), session.getId()));
}

it has long lines so a bit hard to read, but I hope it's not too bad

then the hrml util looks like this

String imageProductDetailsLink = "";
imageProductDetailsLink += "<a href=\"imageProductDetailsLink += "efashion/new/Controller?command=";
imageProductDetailsLink += "ViewProducts&action=displayDetails&product_id=";
imageProductDetailsLink += Integer.toString(productId);
imageProductDetailsLink += "$jsessionid$";
imageProductDetailsLink += sessionId + "\">";

so this was an attempt to use the session id, but the session itself disappears when I go to the command class
 
Well, any classes beneath your servlet or jsp page will not have access to the session unless you pass it in.

Are you passing the session data to the lower level classes that need access to it? The session is scoped to the request, not to all classes the request accesses..

 
That is the root of the problem, the htmlUtil code you see there is the front end, ie what is output to explorer, the link that is generated that has my local url in it is my gateway to the viewDetails servlet, how can I pass the current "request", ie the one used by my jsp page, through to the htmlutil class so that it can generate a hyperlink that will maintain the request? is this possible or should I take another approach. What is there goes through a controller class...between the initial jsp page and the controller class I loose the session, so I presume I must have created a new request by clicking on the link in explorer.
 
I do not fully understand what you are doing in the controller class, my guess is that the control class calls other classes to do its dirty work and its not passing the session into those classes, thus they 'look' like they are losing the session.

Sorry I can't help more, I use a different methodology when I develop so its difficult for me to be accurate in this tip giving exercise :)
 
The problem occurs before the controller class, the controller class does not receive the session either.
 
So your /efashion/new/Controller path is handled directly by a servlet that is your controller class?

If so, I am stumped.

Good luck, sorry I couldn't help more.
 
the path you mentioned is a mapping in my tomcat web.xml stuff or one of those config files and it accesses the controller class that uses a factory to forward the request to the viewdetails command. so if I could get the info to the control class that would solve teh problem I think
 
If the controller class is referenced directly in the web.xml file it's constructor should look like this

Code:
public void doGet(HttpServletRequest req , HttpServletResponse res) thro
ws ServletException,IOException{

Then when the controller with this constructor forwards to the viewDetails it should send 'req' as an argument and you can access the session through that..

I am probably missing the boat here, wouldnt be the first time.
 
you are totally on the right track, but what I have failed to get across is this:

The jsp page has a session
It generates some code includign a hyper link
you click on the hyper link to boot up the controller class and subsequently the details page

ahh, but wait, you just started a new request by clicking on that link, so the req from the jsp page has not been forwarded.

and that is the problem
 
SOLUTION
I made a newbie mistake. The problem is this: an absolute path in a hyperlink creates a new request and thus a new session. A relative path does not, so instead of "myhost/controller" you have to use "contoller
 
I didn't know that, good catch and I'll watch for it in the future!

Thanks for following up, so many times we never know what the real answer was :(
 
I was under the impression that servlet containers assigned JSESSIONID's on this basis :

- is there a JSESSIONID cookie in the browser ?
- is there a JSESSIONID on the URL ?

So if a browser has a JSESSIONID, then switching URLS should not be a problem ...

Now if a site fires a new browser, then this would cause a new JSESSIONID cookie to be initiated though ...
 
I do not know a lot about JSESSIONID, and again, I am not sure what the relationship between cookies and sessions is. What I do know is this, the session var stores a JSESSIONID and it can be accessed with an appropriately named method in the api. The method returns a nonsensical string as one might expect. This is not publicly displayed in the url in my experience, though from some old posts I read, there may have been something similar to this around the JDK1.2 days.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top