January 28th, 2002, 11:48 AM
Passing a variable on the URL to JSP?
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:
Can anyone tell me how to do this? And how I retrieve the variables inside my jsp file?
Think of the most annoyingly simple question and I've probably asked it on here!
January 28th, 2002, 03:51 PM
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:
This will redirect you to the next page:
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?
January 28th, 2002, 08:45 PM
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 -->
<!-- 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.