How to use the Session object to control page flow in an application.
You can run this technique code from:
NOTE First make sure that database is running on your localhost SilverStream Server | |
See the chapter on programming pages in the Programmer's Guide |
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:
This code is written in pageRequestBegin() and it involves:
About the servlet
The technique shown in the servlet is:
This code involves using parameters in a servlet request to create content for an URL.
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"); }
The following code removes the FLOW session value.
private void handle_btnRemove_pageActionPerformed(ActionEvent evt) throws Exception { removeSessionValue("FLOW"); }
removeSessionValue()
and supplying the name of the value.
removeSession()
in pgFlow1, you are providing the same information to pgFlow2 as in the case where the user has not previously visited pgFlow1. That is, in both cases, FLOW is not set.
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"); }
showPage()
to redirect the request to the pgFlow2 page. The code in pgFlow2 could further redirect the request.
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); } }
showpage()
and specifying the URL for redirection as the first parameter. In this case, the URL specifies a business object with a servlet trigger. The server will produce a servlet trigger in response to this HTTP request. Also supply (in the fourth parameter) the Hashtable that contains the information that will be used by the servlet's service()
method to provide some of the content of the error 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"); }
showPage()
lets the server tell the browser to retrieve the requested document from pgFlow1.
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; } }
request.getParameterValues()
. This value consists of the string that you entered into the Hashtable in pgFlow2. It will serve as the error message.
getParameterValues()
, that is, message[0]. Call append()
to first create, then add HTML commands to the string buffer that will represent the content of the HTML document.
getParameterValues()
to get the values associated with the URL1 and LINK1 parameters from the servlet request. Use them to extend the content of the HTML document to include a text that is linked to pgFlow1.