0

I am using following code to get session variable value in javascript embedded in a JSP:

var numberOfPages= '<%=session.getAttribute("numberOfPage")%>';

I am getting a wrong value (old value) when I hit the page for the first time but when I refresh the page, then I'm getting the correct value. Please help me.

Servlet code:

HttpSession session = request.getSession(false); 
if( session == null ) { 
   session = request.getSession(true); 
} 
String numberOfPage = Integer.toString(noOfPages);   
session.removeAttribute("numberOfPage"); 
session.setAttribute("numberOfPage", numberOfPage);

Jsp (javascript) Code:

if(<%=session.getAttribute("numberOfPage")%>!= null)
numberOfPages= '<%=session.getAttribute("numberOfPage")%>'; 
7
  • 3
    Java or JavaScript? Pick the language in which the issue resides (also, tag with JSP or whatever you're using). Commented Nov 15, 2013 at 6:55
  • what is that wrong value ? is it null? Commented Nov 15, 2013 at 6:58
  • first time wrong value means are you getting any values or not?Unless session is having any value,you can not recieve it getAttribute() Commented Nov 15, 2013 at 6:58
  • Not null. getting old value. At the time of set session i have removed that attribute and then set attribute but still it giving old value in jsp Commented Nov 15, 2013 at 7:00
  • 1
    Edit your question with the code. It's completely unreadable in a comment. Commented Nov 15, 2013 at 7:13

1 Answer 1

1

session.getAttribute() returns an Object, try to cast it.

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.