aspose file tools*
The moose likes JSP and the fly likes Setting a JavaScript Variable to Session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Setting a JavaScript Variable to Session" Watch "Setting a JavaScript Variable to Session" New topic
Author

Setting a JavaScript Variable to Session

Divya Chandrasekhar
Greenhorn

Joined: Jun 04, 2011
Posts: 21

Hi All,

I have an issue with setting a JavaScript variable to session. This is a string and I need to retrieve this in my Action Class and process further. I am using Struts 1.1.
I have tried setting this variable as a hidden variable however, it doesn't reach the Action Class - I get a null value.

Any suggestions on what I would need to do?

Here's the gist of what I am trying to do:

JSP:
<html>
<body>
<input type="hidden" name="userInfo" />
<input type="submit" onclick="submit();" value="Submit">
<input type="submit" onclick="cancel();" value="Cancel">

<form>
....
....
....
//make a call to foo()
</form>
<script>
function foo() {
var bar = "Divya";
document.getElementsByTagName("userInfo").value = bar;
}
</script>
</body>
</html>


SCJP 6: 96% SCWCD 5: 98%
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Just to be sure, Did you have the variable"userInfo" in the actionform?? And how are you accessing this value in the action class??
Please put an alert below the code and see what is being printed.
Divya Chandrasekhar
Greenhorn

Joined: Jun 04, 2011
Posts: 21

the alert prints the value "Divya"... checked this
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
bhanu chowdary wrote: Did you have the variable"userInfo" in the actionform?? And how are you accessing this value in the action class??

You didn't answer these questions
Divya Chandrasekhar
Greenhorn

Joined: Jun 04, 2011
Posts: 21

My bad
To answer your questions:
1. I have a variable userInfo in the action form
2. I am accessing the value by doing a String str = request.getParameter("userInfo");
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
Its been a while since I have worked on struts. I remember encountering the same issue once.
Could you please try it with <html:hidden> tag instead of <input type=hidden>?
I don't have my workspace setup to work with struts now. But its worth giving a try

This should be inside the form tag
Divya Chandrasekhar
Greenhorn

Joined: Jun 04, 2011
Posts: 21

Thanks bhanu, will try it out and let you know...
Divya Chandrasekhar
Greenhorn

Joined: Jun 04, 2011
Posts: 21

No luck
Any help will be appreciated!
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi Divya,

My suggestion will be to try to convert your post request to get and see if that parameter is appended to your URL.

if not, there is only one logical reason that your parameter "userinfo" is not a part of the form that is submitted.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting a JavaScript Variable to Session