Parsing url parameters in a JSP using Java ?

To start off, I've passed several string variables to a JSP page by appending them to the url as shown:

location.href="transUpdate.jsp?Var1="+transactionId+"&Var2="+globalCurrentUser+"&Var3="+timeNow+"&Var4="+idString+"&Var5="+countString+"&Var6="+globalShippingCost+"&Var7="+globalTotalTransCost+"&Var8="+globalPaymentType;


I initially used the following javascript function in transUpdate.jsp to parse the url for individual values:


function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
            
  for (var i=0;i<vars.length;i++)
  {
    var pair = vars[i].split("=");
                  
    if (pair[0] == variable)
    {
      return pair[1];
    }
}

...where variable = "Var1", "Var2", etc.

The problem is that the whole purpose of passing them to the jsp page is to connect to a database (using Java) and use them to update several table values. Having the strings that I need in javascript is fairly useless here as I don't believe you can assign one to the other. On the other hand, I can't figure out how to reference the current page's url using Java.

Basically, what I'm looking for is either a function written in Java that's equivalent to the javascript function above, or a roundabout way of assigning the javascript values to java variables.  

As usual, any comments are greatly appreciated.
AtomskishAsked:
Who is Participating?
 
sompol_kiatkamolchaiConnect With a Mentor Commented:
put it in JSP Declaration like this and then you can use it anywhere in JSP

Declaration
=======
<%!
  private String getQueryVariable(String name) {
    return request.getParameter(name);
  }
%>

Use It.
====
<%=getQueryVariable("Var1")%>
0
 
sompol_kiatkamolchaiCommented:
private String getQueryVariable(String name) {
  return request.getParameter(name);
}
0
 
petmagdyCommented:
the JSP that receive the url parameters will look like this:

<%@page contentType="text/html" import="java.util.*" %>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>JSP Page</title></head>
<body>

<%-- <jsp:useBean id="beanInstanceName" scope="session" class="beanPackage.BeanClassName" /> --%>
<%-- <jsp:getProperty name="beanInstanceName"  property="propertyName" /> --%>

<%
 MAP map = request.getParameterMap();
 Set keys = map.keySet();
 Iterator itr = keys.iterator();
 while(itr.hasNext())
 {
  Object key = itr.next();
  Object value = map.get(key);
  out.println("<p>Key: " + key.toString() + " value: " + value.toString() + "</p>");
 }
%>
</body>
</html>
0
 
petmagdyCommented:
correction:

<%@page import="java.util.*" %>
<%@page contentType="text/html"  %>
<%@page pageEncoding="UTF-8"%>
<html>
<head><title>JSP Page</title></head>
<body>

<%-- <jsp:useBean id="beanInstanceName" scope="session" class="beanPackage.BeanClassName" /> --%>
<%-- <jsp:getProperty name="beanInstanceName"  property="propertyName" /> --%>

<%
 Map map = request.getParameterMap();
 Set keys = map.keySet();
 Iterator itr = keys.iterator();
 while(itr.hasNext())
 {
  Object key = itr.next();
  String value = (String) map.get(key);
  out.println("<p>Key: " + key.toString() + " value: " + value + "</p>");
 }
%>
</body>
</html>
0
 
AtomskishAuthor Commented:
I guess the answer was a lot simpler than I expected. petmagdy, your page compiles okay, but gives a server error when it tries to display. The problem's probably on my end, so thanks for your efforts.
0
All Courses

From novice to tech pro — start learning today.