Application Techniques



Controlling Page Flow Using the Session Object

How to use the Session object to control page flow in an application.

About this technique

Details

Category

HTML Client Techniques> Page flow and state management

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on programming pages in the Programmer's Guide

Walking through the flow scenario   Top of page

The example uses two HTML pages and a servlet. The user must first visit pgFlow1 in order to be able to visit pgFlow2. The code in pgFlow1 sets the Session object's FLOW value. The code in pgFlow2 checks if FLOW is set. If it isn't, pgFlow2 sends a redirect response to the client using the URL of an error message servlet. The servlet uses the parameters that pgFlow2 passes it to insert content into the error document, which is returned as a response using the output stream.

About the first page

The techniques shown in the first page (pgFlow1) are:

About the second page

The techniques shown in the second page (pgFlow2) are:

About the servlet

The technique shown in the servlet is:

Setting a session value   Top of page

The following code shows the code that runs when the page is loaded. It set the Session object's FLOW value. This same code is used to handle the event in which the Set session value button is clicked.

  //Runs when page is loaded 
  protected void pageLoaded(AgiHttpServletRequest req, AgiHttpServletResponse res) throws Exception 
  { 
     setSessionValue("FLOW", "Been to pgFLOW1"); 
  } 
   
  //Runs when Set session value button is clicked 
  private void handle_btnSet_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     setSessionValue("FLOW", "Been to pgFLOW1"); 
  } 

Notes about the code

Removing a session value   Top of page

The following code removes the FLOW session value.

  private void handle_btnRemove_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     removeSessionValue("FLOW"); 
  } 

Notes about the code

Redirecting an HTTP request to another URL   Top of page

The following code lets the server tell the browser to retrieve the requested document from a different URL location. It provides the pgFlow2 page as the new URL.

  private void handle_btnGo_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     showPage("pgFlow2.html"); 
  } 

Notes about the code

Redirecting an HTTP request upon page loading   Top of page

The following code implements the pageRequestBegin() method of the pgFlow2.html page. This method runs immediately after the page is loaded. The code checks if the session value FLOW is set. If FLOW is set, it does not redirect the request. Otherwise, it tells the browser to retrieve the document (in this case, the error message page) from a different URL. The code creates a Hashtable to contain the parameters

  protected void pageRequestBegin(AgiHttpServletRequest req, AgiHttpServletResponse res)  
     throws Exception 
  { 
     String value = (String) getSessionValue("FLOW"); 
     if (value == null) 
     { 
        // No value for FLOW in Session object, so redirect to error page. 
        Hashtable message = new Hashtable(); 
        message.put("MESSAGE", "This page can only be accessed via pgFlow1.html."); 
        message.put("URL1", "SilverStream/pages/pgFlow1.html"); 
        message.put("LINK1", "Go to pgFlow1"); 
        showPage("../../message.html", null, null, message); 
     } 
  } 

Notes about the code

Redirecting the HTTP request to the first page   Top of page

The following code redirects the http request to the pgFlow1 URL. This code runs when the user clicks the Return to pgFLOW1 button.

  private void handle_btnReturn_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     showPage("pgFlow1.html"); 
  } 

Notes about the code

Writing the error message servlet   Top of page

The following code shows the service() method in the messageServlet business object. This business object has a servlet trigger for the URL (message.html) to which pgFlow2 has redirected the HTTP request.

When the server produces a servlet trigger in response to the HTTP request to the message.html URL, this listening business object (messageServlet) is instantiated, and the server executes the servlet's service() method.

  public void service(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res)  
     throws javax.servlet.ServletException, java.io.IOException 
  { 
     StringBuffer html = new StringBuffer(); 
      
     AgoHttpRequestEvent request = (AgoHttpRequestEvent) req; 
     AgoHttpReplyEvent response = (AgoHttpReplyEvent) res; 
      
     String[] message = request.getParameterValues("MESSAGE"); 
     if (message != null) 
     { 
        System.out.println("messageServlet (service) GOT VALUE FOR MESSAGE"); 
        if (message[0] != null) 
        { 
           System.out.println("messageServlet (service) message = "  
              + message[0]); 
         
           html.append("<HTML><HEAD></HEAD&rt;><BODY> 
              <B><FONT SIZE=4 COLOR=\"#2C547C\" FACE=\"Arial\"> 
              <SPAN STYLE=\"font-size:20;\">"); 
           html.append(message[0] + "</SPAN></FONT></B><P><P>"); 
            
           String[] url = request.getParameterValues("URL1"); 
           String[] link = request.getParameterValues("LINK1"); 
           if (url != null && link != null) 
           { 
              if (url[0] != null && link[0] != null) 
              { 
                 html.append("<A HREF=\"" + url[0] + "\">" + 
                    link[0] + "</A>"); 
              } 
           } 
            
           html.append(""); 
   
           OutputStream out = response.getOutputStream(); 
           out.write(html.toString().getBytes()); 
           response.setContentType("text/html"); 
           response.setStatus(response.SC_OK); 
        } 
        return; 
     } 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.