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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.