my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes Jamming javascript variable into the session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Jamming javascript variable into the session" Watch "Jamming javascript variable into the session" New topic
Author

Jamming javascript variable into the session

Greg Werner
Ranch Hand

Joined: May 07, 2009
Posts: 54
Hi all,

I know that because the session resides on the server javascript is client, storing javascript in the session does not make sense. However, here is my situation ...

I have a value passed into a javascript function on a jsp page. This is a comma delimted string which could potentially be very very long. I need to pass it to another jsp page. The previous design, which I am correcting, sets the long string as a parameter of a form that gets submitted. However, when the string is x characters long or greater, the info will not be passed and we will end up with a blank white screen. Therefore, it can not be part of the form as it is now because x characters or greater is allowable in our situation. That is why I would like to put it in the session so that I can scoop it up on the next jsp page. Any ideas?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

If the form method is POST rather than GET, you shouldn't run into any length issues.

To go the session route, you'd need to use Ajax to submit the value to the server behind the scenes.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Greg Werner
Ranch Hand

Joined: May 07, 2009
Posts: 54
I have attached the code. It is a POST, but I don't think it reliably fails on all browser/server combos, just the one that happens to matter I would be interest hearing more about the AJAX route. By the way, the bold line is where I am trying to stuff info into the session. It happens to execute because the authors of the code happened to also create a Java variable called sCommand in the jsp file (which does not have the correct value), how convenient.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

If the method is post, I don't see how you are hitting any length restrictions.

Using jQuery for Ajax (life is just too short to do Ajax "by hand"), something along the lines of:

should do the trick (assumes JSP to get the context path).

This will post back to a servlet with parameters for the name and value that the servlet can create in the session/
Greg Werner
Ranch Hand

Joined: May 07, 2009
Posts: 54
Thanks for the example. We weren't using jQuery in the past, but I guess we are now as I have implemented that and downloaded the jQuery js file. Just a small question of clarification. It is my understanding that when I use jQuery.post as you have showed me, I would then reference the name as request.getParameter(name) where name is the name I have selected for my data ('gmids' was my example). Is this correct? I have sort of a double submit example which is why I was thinking session. The code I demonstrated prepares a form to be submitted so first I was trying to store that 'gmids' in a session (which is post #1 via jQuery) and then submit the generated form (post #2) which would clear the parameter I just set in jQuery post. Would you recommend I try to combine the two posts . The remained of the original function is:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Sorry, can't follow all that. How about backing up and describing what you are actually trying to accomplish. It sounds like you may be over-complicating the whole thing.
Greg Werner
Ranch Hand

Joined: May 07, 2009
Posts: 54
Trying to pass a long list of dynamically generated ids from one jsp to another. The page which generates the ids has its form dynamically generated. Therefore, I need to dynamically generate the form and in the process include the ids before executing a submit to navigate to the next jsp. I can not include the ids directly in the form, because there are too many and a white screen would result. I would like to pass the ids separately from the form itself because the creation of the form is so complex/involved.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

I still think that you are over-complicating things, but since you haven't said anything about what you are really trying to accomplish with all this, I have no suggestions for simplification.

So if:
Greg Werner wrote: The page which generates the ids has its form dynamically generated.

Why aren't you just putting the id values in the session when the form is generated? Why wait until after its been sent to the browser?

I strongly suggest we address the strategy rather than the tactics. I feel you are building a Rube Goldberg machine to do something that could be much simpler.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jamming javascript variable into the session
 
Similar Threads
Passed
Passed!
Passed IBM 483
Purging attributes from HttpSession
JSP security: potential issue with arguments passed to a JSP?