Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Thanked 0 Times in 0 Posts

    jsp passing parameter to another jsp

    Hi,may I know how can I pass parameter from one jsp to another jsp?

    Currently,I has a string called "s" in my booking.jsp,what should I write the code so that "s" can be pass to Payment.jsp?

    And in Payment.jsp,what should I declare so that it can accept the value pass from booking.jsp?


  • #2
    New Coder
    Join Date
    Aug 2008
    Bismarck, ND, USA
    Thanked 0 Times in 0 Posts


    That's a pretty simple operation; I do it heavily from within a loaded JSP fragment to other 'subroutines' within itself (separated with the control structure I'll show below) and with other JSP fragments/pages to be loaded upon submission of a form.

    Here's the way that I pass a value from a JSP calling for another one to be loaded. My example is going to be a little bit more complicated than what you want, I believe, because I am using an index.jsp main page which I submit my options to. index.jsp tests for a value of ${sub} in order to find out the page that it should load in a certain area of its layout; the value of ${sub} is taken to be the name of the next JSP to fill in that area. ie ${sub}.jsp is the name of the page to load; that page then loads the other values named in parameters other than ${sub} as any parameters it needs to pay attention to.

    Actually pay no attention to my babbling about the index.jsp page calling the other ones. I'll simplify the code instead of just cut 'n pasting it here; it won't take me long. That does show some of the things that you can do with passing those values, however, if you're curious. You can implement a pseudo-subroutine kind of structure after passing a value to a page as simply as:

      <c:when test="${param.sub == 'foobar'}">
        <%-- code to execute 'foobar' subroutine --%>
        <%-- etc, etc, etc --%>
    So here is how to pass. First example is your calling JSP fragment:

    <%-- Embedded in whatever JSP you have to this point --%>
      <select name="foobar">
        <option value="test1">Option One</option>
        <option value="test2">Option Two</option>
      <input type="hidden" name="nextFoobar" value="orc" />
      <input type="submit" name="foobar3" value="${param.sentFromSomewhereElse}" />
    Your 'receiving' JSP fragment will test for these values in the following:
    <%-- Embedded in whatever JSP/HTML you have to this point for formatting, etc --%>
      <li>Your <i>'foobar'</i> selection was: ${param.foobar}</li>
      <li>Your <i>'nextFoober'</i> was set to: ${param.nextFoobar}</li>
      <li>Your <i>'submit'</i> button returned: ${param.sentFromSomewhereElse}</li>
      <c:when test="${param.sentFromSomewhereElse == 'true'}">
        <p><b>Your JSP fragment with the embedded form code had sentFromSomewhereElse set to the string value of 'true' which means that the <i>calling form</i> was originally called from <i>another</i> JSP fragment which set that value and submitted it in order to load that form.</b>
        <p>Your JSP fragment containing your embedded form was not called from another JSP or HTML file which initially set sentFromSomwhereElse to the string value of 'true'
    So, as you can see, basically when you send an HTML form value, it sets the JSP value of ${param.whatever} accordingly. If you're not looking to set these values from a form, but simply from within JSP scripting tags, you'll want to look into jsp:setProperty and jsp:getProperty; if you're setting these values from within an embedded java scriptlet within the JSP, you'll want to look into x .getAttribute and x .setAttribute, I believe. If the get/set JSP tags and java methods are not exactly correct I am definitely open to correction from others; otherwise they should be documented on www.sun.com in the JSP API documentation.

    Hope this helps you out a bit.

    Happy Xmas eve if you're part of the world that is into that sort of thing.

    -Damon Getsman
    Programmer/IT Customer Relations/Linux & Solaris Sysadmin


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts