|
Server-Side Includes with Logic |
|
|
Now that we have laid the groundwork for using server-side include files, it's time to
make further use of them by including logic, not just HTML or plain text.
|
||
|
This lesson will demonstrate how put logic into include files.
|
||
|
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 J4 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: 3. Let's first create the navigation include file. The logic of this file would be as follows:
<%
Save this file as j2ee_navigation5.jsp.
signon := session at: 'signon' ifPresent: [ :signon | response write: '<br><br>'. response write: '<a href=j2ee_employeecourses5.ssp>List Your Courses</a><br><br>'. ((signon level) asNumber > 1) ifTrue: [ response write: '<a href=j2ee_allemployeecourses5.ssp>List All Employee Courses</a><br><br>'. response write: '<a href=j2ee_listbycourse5.ssp>List Employees By Course</a><br><br>'. ]. ((signon level) asNumber > 2) ifTrue: [ response write: '<a href=j2ee_addcourses5.ssp>Add Courses</a><br><br>'. response write: '<a href=j2ee_employees5.ssp>List Employees</a><br><br>'. response write: '<a href=j2ee_addemployees5.ssp>Add Employees</a><br><br>'. ]. ]. session at: 'signon' ifAbsent: [ response write: 'Your login session has expired. Please <a href=logon5.ssp?msg=222>login</a> again'. ]. %> Note that the file extension is jsp as opposed to inc. The J2EE specification states that if an include file contains logic (code), it must be a file that the J2EE engine can recognize as having code it that must be parsed. As such, the include file must have an extention of either ssp or jsp. In this tutorial, the extention jsp will be used to distinguish it from a "real" SSP page. 4. There will not be any changes to the logon page so for now, simply change all references of "4" to "5" and save it as logon5.ssp. 5. We will make a slight change to our verify servlet. Start with ServletVerify4 and make it look like the following (the changes have been highlighted). Save this as ServletVerify5.
doPost: aRequest response: aResponse
The first change is by making a reference to a session if one exists. If there is a session
named signon, it will be removed (i.e. we will remove any previous value stored
in our session variable). This is to assure a proper login. Otherwise, if you had previously
signed in correctly, and tried to login again using invalid values, you would still be sent to
the home page because the session variable would still be present.
| firstName lastName passWord goodLogin employees mySession | firstName := aRequest anyFormValueAt: 'firstName'. lastName := aRequest anyFormValueAt: 'lastName'. passWord := aRequest anyFormValueAt: 'passWord'. mySession := aRequest session. mySession removeKey: 'signon'. goodLogin := 'NO'. employees := (Toyz new) getEmployees. employees do: [ :each | (passWord = each number) ifTrue: [ (lastName = each lastName) ifTrue: [ (firstName = each firstName) ifTrue: [ goodLogin := 'YES'. mySession setAttribute: 'signon' to: each. mySession timeout: 5.]. ]. ]. ]. (goodLogin = 'YES') ifTrue: [ aResponse redirectTo: '/teach/main5.ssp'. ] ifFalse: [ aResponse redirectTo: '/teach/logon5.ssp?msg=111'.]. The other higlighted code 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! 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 will not spend much energy on this since it mainly cosmetic. You will see the changes they made shortly. 7. The biggest change will be to our home (main) 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:
<jsp:include page="j2eeheader5.inc" flush="true" />
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.
<table width=90% border=0> <tr> <td width=200 align=center> <jsp:include page="j2ee_navigation5.jsp" flush="true" /></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> <jsp:include page="j2eefooter5.inc" flush="true" />
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. From the System Browser, <Operate-Click> the category of ToyzInc and select File Out As.... Enter ToyzInc_j5.st as the filename. |
||
|
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:
|