• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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?
0
NASEEMA
Asked:
NASEEMA
  • 2
1 Solution
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now