JSP SCRIPTLET

The JSP_SCRIPTLET element defines JSP scriptlet code that should be inserted into the page at that point relative to any other LIST or CLUSTER elements. Any TextHelper beans declared by a SERVER_INTERFACE element to be in the DISPLAY phase are available to the scriptlet by getting the attribute of the page context with the same name as the NAME attribute of the SERVER_INTERFACE element. An example is shown in JSP SCRIPTLET below.

Figure 1. Example JSP SCRIPTLET Accessing a TextHelper
<SERVER_INTERFACE NAME="MyBeanName" CLASS="MyClass"
                  OPERATION="getMyData" />
<JSP_SCRIPTLET>
  <![CDATA[
    curam.omega3.texthelper.TextHelper th =
        pageContext.findAttribute("MyBeanName");
    String myValue = th.getFieldValue("myPropertyName");
    out.print("VALUE: " + myValue);
  ]]>
</JSP_SCRIPTLET>

As the code within the JSP_SCRIPTLET element may contain reserved XML characters1, you can either replace these characters with the appropriate XML character entity or enclose the contents of the element in the CDATA ("character data") block as shown above which will prevent the XML parser from trying to interpret the contents of the block.

A common use of the JSP_SCRIPTLET element is to write code that will redirect the current page to another page. JSP SCRIPTLET, below, shows an example of this.

Figure 2. Example JSP SCRIPTLET Redirecting to a Page
<PAGE PAGE_ID="Activity_resolveAttendeeHome">
  <JSP_SCRIPTLET>
    <![CDATA[
      curam.omega3.request.RequestHandler rh
          = curam.omega3.request.RequestHandlerFactory
                 .getRequestHandler(request);
      String context = request.getContextPath() + "/";
      context += curam.omega3.user.UserPreferencesFactory
                     .getUserPreferences(pageContext.getSession())
                     .getLocale() + "/";
      String url = context + "UserCalendarPage.do?"
                           + "startDate=&calendarViewType=CVT3";
      url += "&" + rh.getSystemParameters();
      response.sendRedirect(response.encodeRedirectURL(url));
    ]]>
  </JSP_SCRIPTLET>
</PAGE>

This demonstrates the API used to access the system parameters that control an application's ability to return to previous pages. The information about the previous page is stored in the system parameters accessible via the RequestHandler. getSystemParameters() method. By adding the system parameters, any Cancel button on the following page will return to the expected page when clicked. The RequestHandlerFactory. getRequestHandler() method is passed the JSP request object and will return the appropriate request handler. The system parameters should be appended to the redirect URL and just require a separating "&" character as they are already formatted in name = value pairs.

When using a JSP_SCRIPTLET to redirect to another page, the JSP_SCRIPTLET should be the only child element of the PAGE element. When this is the case, no HTML content will be generated for the page: it will not be displayed, so no HTML is required. If other elements are present, then HTML content will be generated. This can include the page header, navigation menus, footer, title, etc. If this HTML content exceeds the size of the buffer on the web container serving the page, then the content will be transmitted to the web browser. Once any content is transmitted in this way, the redirect operation will have no effect. Therefore, ensuring that the page contains a single JSP_SCRIPTLET element and no other elements will ensure that the redirect operation works as expected.

If you need to access a TextHelper instance from a JSP scriptlet that redirects to another page, then you cannot use the SERVER_INTERFACE element to declare the TextHelper as shown in JSP SCRIPTLET, as this extra element would cause HTML content to be generated. Instead, you must declare the TextHelper instance within the scriptlet code as shown below.

It should be noted that, when using JSP_SCRIPTLET, there is limited error handling capability. Thus, code should not make calls to secured server interface methods. Instead, the target page of any JSP_SCRIPTLET should be secured appropriately.

Figure 3. Example JSP_SCRIPTLET Redirecting and Accessing a TextHelper
<PAGE PAGE_ID="Activity_resolveApplicationHome">
  <JSP_SCRIPTLET>
    <![CDATA[
      curam.omega3.request.RequestHandler rh
          = curam.omega3.request.RequestHandlerFactory
                 .getRequestHandler(request);
      String context = request.getContextPath() + "/";
      context += curam.omega3.user.UserPreferencesFactory
                     .getUserPreferences(pageContext.getSession())
                     .getLocale() + "/";
      String activityID = request.getParameter("ID");
      String eventType = request.getParameter("TYPE");
      String url = context;

      curam.interfaces.ActivityPkg.Activity_readDescription_TH
          th = new curam.interfaces.ActivityPkg
                        .Activity_readDescription_TH();
      th.setFieldValue(
          th.key$activityDescriptionKey$activityID_idx,
          activityID);
      th.callServer();

      String description = th.getFieldValue(
          th.result$activityDescriptionDetails$description_idx);
      if (eventType.equals("AT1")) {
        url = "Activity_viewUserRecurringActivityPage.do?";
      } else {
        url = "Activity_viewUserStandardActivityPage.do?";
      }
      url += "activityID=" + activityID;
      url += "&description="
             + curam.omega3.request.RequestUtils.escapeURL(
                   description);
      url += "&" + rh.getSystemParameters();
      response.sendRedirect(response.encodeRedirectURL(url));
    ]]>
  </JSP_SCRIPTLET>
</PAGE>

When adding parameters to the parameter list, care must be taken if the parameter value may contain non-ASCII characters. Values containing non-ASCII characters must be escaped before they are added to the parameter list to ensure that the characters are preserved correctly. The RequestUtils. escapeURL(String) method can be used to perform the escaping. An example of the Java code to perform this escaping is shown in the example above. Code following that pattern should be included within your JSP scriptlet.

1 The reserved characters in XML are " ' ", " " ", " & ", " < ", and " > ". The respective XML character entities are " &apos; ", " &quot; ", " &amp; ", " &lt; ",and " &gt; ".