Allowing the User to Choose the Style Sheet
Now that we have the basics of server-side processing, it's time to give the user control.
Part of the real power of server-side processing comes when the user is able to choose the style sheet and control the look-and-feel of the site. To do this, we'll build a "preferences" form page that shows the user his or her choices and when submitted, saves the choice in a cookie. This cookie can then be accessed from future pages to determine how the page is displayed. (If you set an expiration date on the cookie, it will even stay in effect for the user's next visit to your site!)
First, we'll create the form:
<html> <head><title>Set preferences</title></head> <body> <form action="prefs_action.asp" method="post"> <h2 style="text-align:center">Choose page layout</h2> <input type="radio" name="choice" value="style1.xsl" checked="checked" /> <img src="images/style1.GIF" alt="Layout 1" /> <input type="radio" name="choice" value="style2.xsl" /> <img src="images/style2.GIF" alt="Layout 2" /> <p style="text-align:center"> <input type="submit" value="Choose Layout" /> </p> </form> </body> </html>
For the ASP implementation, save this file as prefs.html in the main Web directory.
For the Java implementation, the page is the same, except that the action is "/examples/servlet/prefs_action" instead of "prefers_action.asp". Save the file as prefs.html in webappbs/root.
We'll display this page as a popup window, so we don't need to add any interface elements. Instead, the form simply provides the two possible options, as shown in Figure 2. To see it, click the Change Layout link on the main page.
Figure 2 The preferences form allows the user to choose his or her preferred layout.