|
Fundamentals of Smalltalk Server Pages (SSP) |
|
|
We're about to create our first Smalltalk Server Page (SSP). Our page will display a list of
employees. In order to do this, the SSP will need to read the employee file and format that
data in a way that a web browser can understand. This lesson will give you an idea of how
this can be done in two different ways.
|
||||||||
|
This lesson will demonstrate how to display the list of employees in a web page using the
ASP model. It will first describe a solution where all you need is a single SSP. It will
then explain how to use a combination of SSP and custom objects. The idea is to get a good
understand of where Smalltalk code can reside in the ASP model.
|
||||||||
|
1.
If VisualWorks is not already running, please start running it now, load the Web Toolkit
parcel and start a Wave HTTP server.
2. We need a place to store the 3 files used to support this application. Under both the sandbox and teach directories, create two directories called db and images.
Figure 1. The db and images sub-directories Place the Toyz Inc logo in the images directory and the 3 text files in the db directory. You can find the image file and the 3 text files at the following links: 3. Everything is now in place for coding to start. But before you do that, you first need to understand what you are trying to do. The goal of this lesson is to write code that generates the following output in your browser:
Figure 2. Desired page 4. If you were to do a "view page source" from the browser, you would see the following HTML:
Figure 3. Desired HTML 5. If we closely examine this HTML document, we will notice that it has some interesting characteristics:
Figure 4. Areas in blue tend to repeat
Figure 5. Code to loop through the employee file and display it in the Transcript All we need to do is tweak the workspace code so that it “plays in an HTML world”. In other words, we need to place HTML tags around our data. In terms of SSP, this would requires a means by which data can be written to the HTML output buffer from within a code block. That means is the ASP model's intrinsic response object.
response write: 'Your HTML output stream goes here'.
7.
Let's take the HTML file, keep everything that was colored in yellow, and let Smalltalk
generate the HTML that was colored in blue. The result would be the following:
Figure 6. A total "SSP" solution
Figure 7. The "SSP" solution as displayed in our browser Note that employee information is being pulled from the employee (text) file. Go ahead - edit the employee file and add a fourth (or fifth) employee and refresh the page. You will see that this page will return all employees listed in the file. A "View Source" or "View Page Source" from the browser will reveal no Smalltalk code – just HTML. 8. In the ASP model, this is a typical situation. A Smalltalk Server Page will contain a mixture of both HTML and code.
Figure 8. A Diagram depicting the simple ASP model Pros Cons 9. A better approach would be to create custom classes and methods (let the logic reside there) and use SSP to display the data.
Figure 9. A better approach to the ASP model
Figure 10. The custom classes for our employee solution
11. Once these classes (and methods) have been created, it will take a lot less code in our SSP to create a list of employees. As you can see, if it doesn't take much less code to retrieve the list of employees in a workspace, the same will be true in the SSP.
Figure 11. A workspace solution 12. Below is a solution that uses a combination of custom classes and SSP. With this solution, there is much less code in the SSP to list employees. In short, objects do the work; SSP does the display.
Figure 12. The custom class solution 13. Finally, a word about readability. The use of response write: is necessary when you need to insert something into the HTML output buffer while you are still within a code block. It is, however, optional. The code samples shown below (2 of them) will generate the same exact output streams. The code sample at the top uses 1 set of percent tags. The code sample at the bottom uses 12 sets of percent tags. All those percent tags (which stops and starts the scripting engine) can sometimes be confusing when mixed with standard HTML tags and normal text. Either code will work but which coding style you choose will simply be a matter of personal preference. Is the code at the top easier to read (therefore debug) than the code at the bottom? You decide.
Figure 13. The matter of readability. |
||||||||
|
Congratulations!
You have successfully written your first SSP page that pulled data from a file and displayed it on the web. And you did this in 2 ways; one using all SSP and another using a combination of SSP and custom classes. You should now be able to:
|
| You now have a choice as to which section you would like to start. You can proceed either to the ASP section or the J2EE section. As stated before, the two sections are completely independent of one another and do not require pre-requisite knowledge from the other section. The language in the tutorials does not assume you have progressed through either section first. As such, much of the concepts will be the same and explained in the same way. The only difference will be in the code used to accomplish a certain task. |