Using XML and XSLT to Personalize a Web Site Part 4: Dynamic Style Sheets and User Customized Layout
Saving User Choices
We'll save the user's portal items in cookies, just as we did with the style sheet choice.
Saving the Choices in ASP
Open prefs_action.asp, and add the changes listed in bold:
<%@ language = "VBSCRIPT" %> <% Response.Cookies("stylechoice") = Request.Form("choice") Response.Cookies("news") = Request.Form("news") Response.Cookies("tvlistings") = Request.Form("tvlistings") Response.Cookies("weather") = Request.Form("weather") Response.Cookies("weatherlocation") = Request.Form("weatherlocation") %> <script type="text/javascript"> self.opener.location=self.opener.location; self.close(); </script>
Just as before, we're simply saving the choices in cookies and then sending the browser Javascript to refresh the main page and close the popup window.
Saving the Choices in Java
To make the changes in Java, make the changes in bold to prefs_action.java, and recompile:
... public class prefs_action extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String stylechoice = request.getParameter("choice"); Cookie styleCookie = new Cookie("stylechoice", stylechoice); response.addCookie(styleCookie); String newschoice = request.getParameter("news"); Cookie newsCookie = new Cookie("news", newschoice); response.addCookie(newsCookie); String tvlistingschoice = request.getParameter("tvlistings"); Cookie tvlistingsCookie = new Cookie("tvlistings", tvlistingschoice); response.addCookie(tvlistingsCookie); String weatherchoice = request.getParameter("weather"); Cookie weatherCookie = new Cookie("weather", weatherchoice); response.addCookie(weatherCookie); String weatherlocationchoice = request.getParameter("weatherlocation"); Cookie weatherlocationCookie = new Cookie("weatherlocation", weatherlocationchoice); response.addCookie(weatherlocationCookie); out.println("<script type=\"text/javascript\">"); out.println(" self.opener.location=self.opener.location;"); out.println(" self.close();"); out.println("</script>"); } }
Just as before, we set the cookies and then output the Javascript that reloads the main page and closes the popup.
Results
To test the changes, save your preferences by submitting the form. You won't see any changes until we've built the final style sheet.