Better was to pass variable

Posted on 2005-04-26
Last Modified: 2010-04-01

I'm Displaying a List of names coming from DB. On selecting any one name it goes to details page.  Right now my code is something like this:
String p = request.getParameter("p")
out.print("<a href=licenseAgree.jsp?sw="+c+"&pro="+p+"&ver="+rs.getString(3)+">" +rs.getString(3) + "</a>");

ie I'm sending all name/value pair as a string over URL. Any other way to send it where user dont see all strings, ?,=, etc., etc.,
If any better way plz provide sample code as well.

Question by:princehyderabad
    LVL 11

    Expert Comment

    U can send in Hidden but for that u need to submit the page..
    LVL 29

    Accepted Solution

    2 ways.
     One is, as karanw, mentions and use a hidden form and a post. Effective but does not hide the variables from the user as they are in the page, they are only not in the url.

    Second, is to use the session object to pass the information.
    session.setAttribute("sw" , c);
    session.setAttribute("pro" , p);
    <a href="licenseAgree.jsp">

    String sw = (String) session.getAttribute("sw");
    session.removeAttribute("sw");//remove if no longer required
    String pro = (String) session.getAttribute("pro");
    session.removeAttribute("pro");//remove if no longer required
    //blah blah blah, your code

    Author Comment

    But its not working properly if <a href is in loop.

    While (....
    <a href="1.jsp?sw=rs.getString(1)">rs.getString(1)</a>

    How can I replace here ? I dont want user to look at a URL String sending ? But same time on 1.jsp if it is not received my page dont work...
    LVL 29

    Expert Comment

    It's just bad syntax

    While (....
    out.println("<a href=\"1.jsp?sw=" + rs.getString(1) + "\">" + rs.getString(1) + "</a>" ;

    you need to be careful of your separation of your Java code and the HTML

    Author Comment

    oops you misunderstood it. I dont mean that what u thinking. U said session.setAttribute("sw" , c); Here "c'" will be changing for each itenration of while loop. correct ?
    so what will session setAttribute of ? for c1 ... or ....c15 ??

    Let assume my output to be in HTML as :

    <a href=1.jsp?sw=c1>c1</a>
    <a href=1.jsp?sw=c2>c2</a>
    <a href=1.jsp?sw=c3>c3</a>
    ...till the end of while loop

    Now tell me how to using session here: session.setAttribute("sw" , ??????);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
    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…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now