Cincom

Extending the use of Session Variables


| Web Toolkit Tutorial Home | Table of Contents | Using Session Variables | Refactoring the ASP solution |
Now that we have laid the groundwork for using session variables, it's time to take advantage of the power they give us.

This lesson will demonstrate how put logic into include files and make more extensive use of session variables.

1. If VisualWorks is not already running, please start running it now, load the Web Toolkit parcel and start a Wave HTTP server. You should also file in the code from Toyz Inc 1 and make sure the initialize method of the Filestuff class sets the correct location of the directory variable (i.e. the directory that contains the 3 data files).

2. In this phase of the application, we will be using 3 include files:


Figure 1. A graphic representation of our web page layout

The 3 include files are:
  • headerX.inc
  • footerX.inc
  • navigationX.inc


  • 3. Let's first create the navigation include file. The logic of this file would be as follows:
    • If the session variable exists, check the employee level
    • If the level is 1 or higher, allow a link to employeecourses5.ssp
    • if the level is 2 or higher, allow a link to allemployeecourses5.ssp
    • and listbycourse5.ssp
    • if the level is 3 or higher, allow a link to addcourses5.ssp and addemployees5.ssp and employees5.ssp
      (even though these pages have not yet been developed)
    • If the session variable does not exist, provide a link to login5.ssp
    Borrowing some code from the logic section of home4.ssp, the file would look something like this:

    <%
    signon := session at: 'signon' ifPresent:
    [ :signon |
      response write: '<br><br>'.
      response write: '<a href=employeecourses5.ssp>List Your Courses</a><br><br>'.
      ((signon level) asNumber > 1) ifTrue:
       [ response write: '<a href=allemployeecourses5.ssp>List All Employee Courses</a><br><br>'.
       response write: '<a href=listbycourse5.ssp>List Employees By Course</a><br><br>'.
      ].
       ((signon level) asNumber > 2) ifTrue:
       [ response write: '<a href=addcourses5.ssp>Add Courses</a><br><br>'.
       response write: '<a href=employees5.ssp>List Employees</a><br><br>'.
       response write: '<a href=addemployees5.ssp>Add Employees</a><br><br>'.
      ].
    ].
    session at: 'signon' ifAbsent:
    [ response write: 'Your login session has expired. Please <a href=login5.ssp?msg=222>login</a> again'. ].
    %>

    Save this file as navigation5.inc.

    4. There will not be any changes to the login page so for now, simply change all references of "4" to "5" and save it as login5.ssp.

    5. We will make a slight change to our validate page. Start with the file validate4.ssp and insert the following two lines of code right after the first percent tag:

    session timeout: 5.
    session removeKey: 'signon'.

    The first line overrides the default lifespan of a session variable of 20 minutes to 5 minutes. This way, you don't have to wait so long to test the timeout logic in our SSP. If you're really impatient, set it to 1!

    The second line of code removes any previous value stored in our session variable. This is to assure a proper login. Otherwise, if you had previously signed in correctly, tried to login again using invalid values, you would still be sent to the home page because the session variable was still present.

    6. The Web Designer has also indicated that there is too much real estate at the top of each page. They want to redesign the header so that it takes up less "vertical" room. We spend much energy on this since this is more cosmetic. You will see the changes they made shortly.

    7. The biggest change will be to our home page. Now that we have created our navigation file, we should be able to piece together a new home page, that once completed, will be a template or working model for all the other pages that we will be creating in this unit. Our new home page will look like the following:

    <%
    title := 'Toyz Inc. Home Page'.
    %>

    <!-- #include file= "header5.inc" -->

    <table width=90% border=0>
    <tr>
    <td width=200 align=center>
    <!-- #include file= "navigation5.inc" -->
    </td>
    <td align=left>
    <%
    signon := session at: 'signon' ifPresent:
    [ :signon |
    response write: ('<h3>Hello ', signon firstName,'</h3>').
    response write: '<h5>Welcome to the TEACH System</h5>'.
    response write: 'Please choose your option from the list of links on the left'.
    ].
    %>
    </td>
    </tr>
    </table>

    <!-- #include file= "footer5.inc" -->

    8. Now would be a good time to test our login process. A good test would be to sign on as all three different employees. Each employee will be given a different set of links on the left side.


    Figure 2. The signon of Anders Anderson - 1 link on the left


    Figure 3. The signon of John Johnson - 3 links on the left


    Figure 4. The signon of Carl Carlson - 6 links on the left

    9. We are now ready to develop a few more SSP pages. However, in order to do that, we must create some new classes and methods.


    Figure 5. The new set of classes and methods needed for our application

    In short, what we have added to our Toyz class are 2 more methods:
  • getCourses - returns a collection of Courses
  • getXrefs - returns a collection of Xrefs (courses employees have taken)


  • You should now file in the code from Toyz Inc 5. We now have enough code to make things interesting on the SSP side.

    10. Let's start off doing something interesting with our session variable. This would be the page employeecourses5.ssp. This page will display a list of courses taken by the person who logged in. The page will look something like the following:

    <%
    title := 'List of your Courses'.
    %>

    <!-- #include file= "header5.inc" -->

    <table width=90% border=0>
    <tr>
    <td width=200 align=center>
    <!-- #include file= "navigation5.inc" -->
    </td>
    <td align=left>
    <table border=1>
    <%
    signon := session at: 'signon' ifPresent:
    [ :signon |
    response write: '<tr><td colspan=4><h5>'.
    response write: (signon firstName).
    response write: ', here are the courses you have taken</h5>'.
    response write: '</td></tr>'.
    toyz := Toyz new.
    courses := toyz getXrefs.
    courses do: [ :each |
    ((signon number) = (each employeeNumber))
    ifTrue:
    [
    response write: '<tr>'.
    response write: ('<td>',each employeeNumber,'</td>').
    response write: ('<td>',each employeeName,'</td>').
    response write: ('<td>',each courseNumber,'</td>').
    response write: ('<td>',each courseName,'</td>').
    response write: '</tr>'.
    ].
    ].
    ].
    %>
    </table>
    </td>
    </tr>
    </table>

    <!-- #include file= "footer5.inc" -->

    The first part of this file is taken directly from home5.ssp. We then change the logic to fit the needs of the page.
  • get a list of all courses taken by all employees (courses := toyz getXrefs)
  • iterate through the list of courses (courses do: [ :each |)
  • test to see if the employee number matches the one found on the xref file
    ((signon number) = (each employeeNumber))
  • if so, create a entry in an HTML table list all four fields from our xref file (the response write: lines)


  • 11. Now would be a good time to test this page. A good test would be to sign on as all three different employees. Each employee has taken a different number of courses.


    Figure 6. The courses taken by employee Carl Carlson

    12. Note that Carl has a link to a page that will display a list of all courses taken by all employees. This one should be very much like the page we just wrote (in fact it should be easier since we don't need to check the file against an employee. It would look something like the following:

    <%
    title := 'All Courses Taken by All Employees'.
    %>

    <!-- #include file= "header5.inc" -->

    <table width=90% border=0>
    <tr>
    <td width=200 align=center>
    <!-- #include file= "navigation5.inc" -->
    </td>
    <td align=left>
    <h5>All Courses taken by all Employees</h5>
    <table border=1>
    <%
    signon := session at: 'signon' ifPresent:
    [ :signon |
    toyz := Toyz new.
    courses := toyz getXrefs.
    courses do: [ :each |
    response write: '<tr>'.
    response write: ('<td>',each employeeNumber,'</td>').
    response write: ('<td>',each employeeName,'</td>').
    response write: ('<td>',each courseNumber,'</td>').
    response write: ('<td>',each courseName,'</td>').
    response write: '</tr>'.
    ].
    ].
    %>
    </table>
    </td>
    </tr>
    </table>

    <!-- #include file= "footer5.inc" -->


    Figure 7. All courses taken by all employees

    At this point, a Toyz object has been instanciated 3 times

    If Carl is the one who signs on and selects all the pages that he can, he will have instanciated the Toyz class 3 separate times.
    1. validate5.ssp
    2. allemployeecourses5.ssp
    3. employeecourses5.ssp
    The objects will not persist across the separate pages; however, this can be solved by storing the first instance of the Toyz class in a session object and then reference that session object on the pages that require a Toyz instance (object).

    This is much more efficient: create an instance of Toyz just once and reuse it in all SSP pages that require it. Here's how to accomplish that:
    • Create the instance in validate5.ssp and store it in a session variable
    • In all other SSP pages, pull the object from the session variable
    • Although it shouldn't be needed, use the ifAbsent: method in conjunction with pulling the object from the session variable - there may have been a timeout
    This is a fair amount of work, therefore, we will leave that for the next lesson.

    Congratulations! Phase 5 of the TEACH application is complete

    We have now made more extensive use of the session variable. With the cookie, all we could do is say hello to them since all that was stored in the cookie was their name. However, the session variable contains an instance of the Employee class, which gives us access to much more than their name.

    You now should be able to:
    Create session variables
    Test for the presence/absense of session variables
    Use session variables like objects

    | Web Toolkit Tutorial Home | Table of Contents | Using Session Variables | Refactoring the ASP solution |