Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Using the User's Preferred Information

Now that we have the user's choice set in a cookie, we can check for it before we display the page.

Checking the Cookie in ASP

Checking the cookie in ASP is straightforward. Save a copy of main_basic.asp as main.asp, and make the following changes:

...
'Set source files sourceFile = Server.MapPath("content.xml") 

if Request.Cookies("stylechoice") = "" then 
  stylesheet = "style1.xsl" 
else 
  stylesheet = Request.Cookies("stylechoice") 
end if 
styleFile = Server.MapPath(stylesheet) 

'Create DOM Documents 
set source = Server.CreateObject("Msxml2.FreeThreadedDOMDocument")
...

Note that the bolding is simply to show what sections have changed.

First, we check for a value in the stylechoice cookie. If there is none, we simply use the original style sheet. Otherwise, we use the cookie value.

Checking the Cookie in Java

Checking the cookie in Java is a bit more complicated. First, change the name of the class to main (rather than main_basic), and resave the file as main.java. After making the changes, compile to main.class.

...
public class main extends HttpServlet {

  public void doGet(HttpServletRequest request, 
           HttpServletResponse response)
...
      //Set source files
      String XMLFileName = "http://localhost/content.xml";
      StreamSource source = new StreamSource(XMLFileName); 
		  
      String XSLFileName = "";
      Cookie[] cookies = request.getCookies();
      if (cookies != null) {
       for (int i = 0; i < cookies.length; i++) {
         Cookie thisCookie = cookies[i];
         if (thisCookie.getName().equals("stylechoice")){
          XSLFileName = 
             "http://localhost/"+thisCookie.getValue();
         }
       }
      }

      if (XSLFileName.equals("")) {
       XSLFileName = "http://localhost/style1.xsl";
      }
      StreamSource style = new StreamSource(XSLFileName);	
      
      //Designate that the result goes to the browser;
...

In Java, cookie objects are retrieved as an array by the getCookies() method. If no cookies are present, this method simply returns null. If there are cookies, we loop through each one, checking the name and saving the value if we find a cookie called stylechoice. (Note that because the binaries and source files are not located in the same directory, we need to provide a full path to the source files.)

Unless an appropriate value is found in this way, we set the XSLFileName variable to point to the original style sheet. In any case, the reference is used to create the style StreamSource object.

The Result

To see this in action, point your browser to http://localhost/main.asp or http://localhost/examples/servlet/main. What you see will depend on the last choice you made on the preferences page. In either case, click the Change Layout link, and select the other layout. When you submit the form, the main page will reload, showing the appropriate layout choice, as shown in Figure 3. In this way, you can change back and forth between layouts without touching your code.

Figure 3 The main page utilizes the style sheet chosen by the user.

  • + Share This
  • 🔖 Save To Your Account