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? Commented May 23, 2014 at 17:12
  • stackoverflow.com/questions/6918314/… Commented May 23, 2014 at 17:22
  • that's not a javascript problem; js has no trouble converting types... Commented 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.

Sign up to request clarification or add additional context in comments.

7 Comments

"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.
i have checked both half(get and set session) are working fine. can you check and tell me the problem you face.
the second half will set the session value before that javascript even reaches the browser.
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 i know the problem in second half? because both are working fine for me
|
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 Comment

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.
0

you can get session value in hidden field in jsp.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.