Skip to main content
Kinetic Community

Get parameters from the URL and request Object

A common pattern in Kinetic CE is to include "GET Parameters" to the URL.   This is useful for developers to easily pass values between pages by adding to the URL.  Then on the next page we can look for the parameter and/or retrieve it's value.  A very common use of GET Parameters in the triangle and base bundles are the "page" parameter which are used to do internal routing within the bundle.

Resolution

In this article we will go over two example of checking for and retrieving URL GET parameters values using JavaScript on the client and how to get reqest parameters in a JSP on the server.

JavaScipt Example:

In the Triangle and Base bundles this function is included:

// Ensure the BUNDLE global object exists
bundle = typeof bundle !== "undefined" ? bundle : {};
    
bundle.getUrlParameters = function() {
   var searchString = window.location.search.substring(1), params = searchString.split("&"), hash = {};
   for (var i = 0; i < params.length; i++) {
     var val = params[i].split("=");
     hash[unescape(val[0])] = unescape(val[1]);
   }
   return hash;
};

To check if your bundle already includes this function open the debug tool in your browser.  Navigate to the console section and enter bundle.getUrlParameters().  If the function is included you should get an object back (ex Object {"": "undefined"}).

To Use (reference Example URLs at bottom of article):

// Check if the URL contains the parameter "page"
if(bundle.getUrlParameters().page !== undefined){
   // Assign the value of "page" to the variable currentPage
   var currentPage = bundle.getUrlParameters().page
}

If we use the examples URLs: below the value of currentPage for the top example would be category and the bottom example would be requests.

JSP Example:

On the server getting the resource to get the value of a parameter on a JSP already exists in the application.  There is no need to add any code to take advantage of the method to get request parameters.

The method that we are going to use are Implicit object defined by the JSP expression language.

  • param: Maps a request parameter name to a single value
  • paramValues: Maps a request parameter name to an array of values

To Use (reference Example URLs at bottom of article):

<%--Check to see if the request contains the parameter "page"
<c:if test="${param['page'] != null}">
    <%-- Assign the value of "page" to the variable currentPage
    <c:set var="currentPage" value="${param['page']}"/>
</c:if>

If we use the examples URLs: below the value of currentPage for the top example would be category and the bottom example would be requests.

Example URLs:

http://localhost:8080/kinetic/internal/catalog?page=category&category=it-request

http://localhost:8080/kinetic/internal/catalog?page=requests