By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

Reading session variable

P: n/a
hi all,

i am working on a java and HTML based web application. now i am storing
session variables. is there a way for me to pass on these variables to
javascript. what i am getting at is, i need JS to do something based on
the user variable.

can somebody help me out on this one.
thanks.

Jan 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Write it out into a hidden and retrieve it from the client. Or simply
create a javascript that contains something like:

out.println("<script>myVar=" + session.getAttribute("id") +
";</script>");

and you have the myVar variable in JS.
At least you get the ideea and can do more nicely depends on what you
are using.

Regards

Jan 17 '06 #2

P: n/a
Write it out into a hidden and retrieve it from the client. Or simply
create a script directly from the server that looks something like:

out.println("<script>myVar=" + session.getAttribute("id") +
";</script>");

and you have the myVar variable in JS.

At least you get the ideea and can do it more nicely depends on what
you
are using.

Regards

Jan 17 '06 #3

P: n/a
the second approach is a bit difficult as i am using separate HTML
pages. the first approach sounds better. but how do you implement it
and what do you mean write it out into a hidden and retrieve it. can
you help more.

Jan 17 '06 #4

P: n/a
If you use separate html pages in frames and so, you can simply modify
it as this.

out.println("<script>top.myVar=" + session.getAttribute("id") +
";</script>");

this will create the variable in the top (the main window object) that
is accessible from every frame by using top.myVar

The other approach would be somewhat simillar, but instead of creating
a variable you create a hidden field in the DOM and you retrieve it by
document.getElementById.

ex:out.println("<input type='hidden' id='mySessionId' value='" +
session.getAttribute("id") + "'/>");

and thus you have an object but this will create it in the same page
that has the process behind it.
You could combine with the first one I described, I presume you have a
login or something where you set all the session variables. At this
stage you can set up all these shared resources too. My advice is to
simply set at this time the top.myVar with the required id and it will
be globally available. If not, deceide on a persistent (something that
lasts and won't change) hidden frame and build thare the objects that
will have the values that you need. Be careful if storing sensitive
data like passwords etc.

Jan 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.