Session variable problem?

hi all ,
I am getting wrong session value.

Here is my code in test.jsp

<a href="2.jsp" onclick="<%session.setAttribute("t","a");%>">a</a>
<br>
<a href="2.jsp" onclick="<%session.setAttribute("t","b");%>">b</a>

and code in 2.jsp is

<%
out.print("session var t="+session.getAttribute("t").toString());
%>

My problem is even i click on a anchor link the out its printing as b.
What could be the problem?
NASEEMAAsked:
Who is Participating?
 
fargoCommented:
onclick should trigger a client side event (javascript etc.) . You cannot have a server side code with onclick event. Setting the session attribute is a server side code..so what u are really doing is not possible (untill unless you use AJAX etc.)

0
 
rrzCommented:
fargo is right, but I would like to explain a little more.  The set methods were executed on the server before the HTML was sent to the browser. Thus the second set method overwrites the first method. That is why you output "b" on  2.jsp  The javascript  onclick method at the browser has on effect on session variables at the server anyway.   One more thing that you should be looking  at is the source code of the page that the browser displays.  In that source code you will see that the code sent to the browser was  
<a href="2.jsp" onclick="">a</a>
<br>
<a href="2.jsp" onclick="">b</a>  
0
 
fargoCommented:
rrz has explained it in detailed. For you to acheive, you need to post a form to set the session variables on some parameter handling etc. You can refer the following link

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21736400.html
0
 
NASEEMAAuthor Commented:
I solved the problem.Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.