The WebRatio pageContext

by Alberto Molinaroli
4,651 views Published on Oct 24, 2011
Applies to: All versions
Table of contents

The WebRatio pageContext

The WebRatio pageContext is a particular data structure which is created everytime the user performs a request in a WebRatio generated Web application. It's a java.util.HashMap that contains information used in the computation of all the Units contained in the requested destination page and it is stored in the JSP Page Context.

This is the typical WebRatio pageContext lifecycle:

  1. The user requests a new page.
  2. The WebRatio pageContext is created. At this point the map contains only information regarding the HTTP request and the HTTP response.
  3. All the input parameters necessary for the Content Units computation are added in the WebRatio pageContext.
  4. The Java page service starts the computation of the Content Units contained in the requested page. For each Content Unit:
    1. It's input parameters are retrieved from the WebRatio pageContext.
    2. Its computation starts.
    3. The Unit State Object resulting from the computation is stored in a new entry of the WebRatio pageContext.
  5. When the Content Units computation is finished, the WebRatio pageContext contains the information regarding:
    • The input parameters of all the Content Units of the page.
    • The Unit State Objects of all the Content Units of the page.
  6. The requested page is served to the user. The <webratio:Page/> tag copies the WebRatio pageContext in the JSP pageContext, in order to make it available through JSTL tags.

This lifecycle makes the WebRatio pageContext available during request's handling and it's accessible to every Content Unit involved in the page computation.

As an example, a Custom Unit or a Script Unit can use the WebRatio pageContext to retrieve the HTTP request with the following code:


 import com.webratio.rtx.RTXConstants

 import javax.servlet.http.HttpServletRequest

 .........

 .........

 HttpServletRequest request = pageContext.get(RTXConstants.HTTP_SERVLET_REQUEST_KEY)

The Java Object "request" can be used to retrieve the information contained in the HTTP request (refer to the HttpServletRequest Java Interface).


 referer = request.getHeader("Referer") // HTTP_REFERER

 userAgent = request.getHeader("User-Agent")  // USER_AGENT

 ip = request.getRemoteAddress()  // IP

The WebRatio Session Context

The WebRatio sessionContext is a particular data structure which is created the first time the user performs a request in a WebRatio generated Web application and is stored in the JSP session of the application. It's a java.util.HashMap that contains information used in the computation of all the units (content and operation) and stores all the session scope objects of a WebRatio application (context parameters and volatile entities).

The WebRatio sessionContext is destroyed when the JSP session is destroyed.

The following code shows how to retrieve the sessionContext in a Custom unit and how to extract the values of the default context parameters:

   import javax.servlet.http.HttpServletRequest;

   import com.webratio.rtx.RTXConstants;

   import javax.servlet.http.HttpSession;

   import com.webratio.struts.WRGlobals;

   import com.webratio.rtx.core.BeanHelper;



   HttpServletRequest request = pageContext.get(RTXConstants.HTTP_SERVLET_REQUEST_KEY);

   HttpSession session = request.getSession();

   Map sessionContext = (Map) httpSession.getAttribute(WRGlobals.SESSION_CONTEXT_KEY);



   String country = BeanHelper.asString(sessionContext.get(RTXConstants.COUNTRY_CTX_PARAM_KEY));

   String language = BeanHelper.asString(sessionContext.get(RTXConstants.LANGUAGE_CTX_PARAM_KEY));

   String currentUser = BeanHelper.asString(sessionContext.get(RTXConstants.CURRENT_USER_CTX_PARAM_KEY));

   String currentGroup = BeanHelper.asString(sessionContext.get(RTXConstants.CURRENT_GROUP_CTX_PARAM_KEY));