#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Northampton, England
    Posts
    82
    Rep Power
    0

    Passing a variable on the URL to JSP?


    Hi,

    I have an HTML file which contains links to some jsp scripts. I would like ot be able to pass a variable using the URL from the HTMl to the jsp script. Can anyone tell me how to do this?

    If you don't know what I mean, this is what I mean in PHP:

    PHP Code:
    <A HREF="index.php?id=223"
    Can anyone tell me how to do this? And how I retrieve the variables inside my jsp file?

    Thanks

    Richard
    Think of the most annoyingly simple question and I've probably asked it on here!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Location
    Belmar, NJ
    Posts
    1
    Rep Power
    0

    Question Similar/ Related Question


    I have a similar question. If there is a series of .jsp forms and you want to set up a link to a certain point by 'forcing' certain answers, is this even possible?

    To see a site with an good example, go to the following link:

    https://ww3.ecenroll.com/ARNE-2002/s...ntid=ARNE-2002

    This will redirect you to the next page:

    https://ww3.ecenroll.com/ARNE-2002/j...tartSearch.jsp

    From here, I want to force the next page in the sequence (as if the user entered the number "1824" and clicked "Search") and the next and so on.

    Is there some way to figure these out?

    Thanks,
    Rand
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2000
    Posts
    1
    Rep Power
    0
    /*
    Note: The following answers the first post. I will tackle the second post below*/

    It's the same as passing it to php as described by your example.

    <!-- copy of your example -->
    <a href="index.jsp?id=223">link</a>
    <!-- end of example copy -->

    To get access to the values in the query string do this:

    <!--assuming an existing jsp page -->
    <%
    String id = request.getParameter( "id" );
    if( null != id && !id.equals( "" ) )
    {
    //do something with it
    }
    %>

    /*End of answer of answer to first post*/

    /*Beginning of answer to second post*/
    Its possible but you need either a servlet or JSP (MVC Patter::Controller) to figure out what and where to forward. Here is a simple example using a JSP fragment

    <%
    String locationID = request.getParameter( "loc" );
    if( null != locationID && !locationID.equals( "" ) )
    {
    if( locationID.equals( "homePage" ) )
    {
    response.sendRedirect( "firstPage?loc=firstPage&nextPage=secondPage" );
    }
    else if( locationID.equals( "firstPage" ) )
    {
    response.sendRedirect( "secondPage?loc=secondPage&nextPage=etc" );
    }
    else if ......
    }
    %>

    It's an incomplete example but what you should get from it is:
    - You need a controller object (JSP or Servlet) to route the request.

    - Setup a format for figuring out what to route and where to route it. In my example I had a parameter called 'loc' that had the value of the page that the user is comming from and another called 'nextPage' that tells controller object where to route the request to. A more elegant solution may be to have a Map that contains all the possible paths that the user can be routed to as the values and the name of the page that triggers the particular path as the key. So the code may look like:
    String path = (String)map.get( request.getParameter( "loc" ) );
    response.sendRedirect( null == path ? "errorPage.jsp" : path );

    I hope that helped.

IMN logo majestic logo threadwatch logo seochat tools logo