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>
<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?
Who is Participating?
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 what u are really doing is not possible (untill unless you use AJAX etc.)

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>
<a href="2.jsp" onclick="">b</a>  
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
NASEEMAAuthor Commented:
I solved the problem.Thanks :)
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.