Session variable problem?

Posted on 2006-04-23
Last Modified: 2012-06-21
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?
Question by:NASEEMA
    LVL 11

    Accepted Solution

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

    LVL 27

    Expert Comment

    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>  
    LVL 11

    Expert Comment

    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

    Author Comment

    I solved the problem.Thanks :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    String format issue 13 86
    build fail in maven project 11 160
    own marker interface 1 42
    if statement not resolving in my code 5 39
    Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now