6

I want to access to session in javascript code so as to set and get some values : i try with this code :

function getsessionvalue() {

    var value= '<%= session["role"].ToString() %>';
    alert(value);
    //var role1= '<%= session["role"] %>'; **the same mistake**
    //alert(role1);     
}

but i have these javascript mistakes for both :

The type of the expression must be an array type but it resolved to 
Type mismatch: cannot convert from String to int
3
  • 2
    The session is and remains server side. If you need something available client side try cookies or simply inserting it on page creation. What server software are you using?
    – EWit
    May 23, 2014 at 17:12
  • stackoverflow.com/questions/6918314/…
    – carter
    May 23, 2014 at 17:22
  • that's not a javascript problem; js has no trouble converting types...
    – dandavis
    May 23, 2014 at 17:36

3 Answers 3

8

you can't access server session in client side. but if you want to do some changes in client side according to server session value. i will give you small idea.it may work for you.

(sorry, I know only java not php,etc.,)

just inside JSP script-let check for the session, create some hidden html element with session value. like this

<% String role=request.getSession().getAttribute("role").toString();%>
<input type="hidden" id="role" value=<%= role ;%> />

And then ,in javascript just get the role from html input element by ID like this.

var role=document.getElementById("role");

and do you stuff here.

And if you want to set role in session in javascript, it may help you

    <script>
function nameYourFunction()
{
    var role="";

    if(your condition)
    <% request.getSession().setAttribute("your variable","your values"); %>
}

</script> 

hope this works. And call your function, whenever you need.

7
  • 1
    "And if you want to set role in session in javascript, it may help you" No, that won't work. the first half of your answer though will work.
    – Kevin B
    May 23, 2014 at 17:55
  • i have checked both half(get and set session) are working fine. can you check and tell me the problem you face. May 23, 2014 at 18:32
  • the second half will set the session value before that javascript even reaches the browser.
    – Kevin B
    May 23, 2014 at 18:33
  • i know that. for that you have to create your javascript function,you need to set when the method will execute, inside the method,you should do all the stuff which i mentioned above. May 23, 2014 at 18:38
  • 1
    may i know the problem in second half? because both are working fine for me May 23, 2014 at 21:18
1

Thru client side javascript also we can access session variables. Following is the simple code which assigns session variable to javascript variable.

<script type="text/javascript">
function getSessionName(){
var name='<%=session.getAttribute("uname")%>';
alert(name);
}</script>

This code worked fine

1
  • While this code snippet may solve the question, including an explanation helps to improve the quality of your response. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. May 10, 2018 at 15:21
0

you can get session value in hidden field in jsp.

<s:hidden id="login_orgId" value="%{#session.org_id}"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.