FileDocCategorySizeDatePackage
PersonalDataCachedServlet.javaAPI DocExample4541Sun Sep 02 14:59:00 BST 2001chap6

PersonalDataCachedServlet

public class PersonalDataCachedServlet extends HttpServlet
A modification of PersonalDataServlet that uses the com.oreilly.javaxslt.util.StylesheetCache class.

Fields Summary
private PersonalDataXML
personalDataXML
private String
editXSLTFileName
private String
thanksXSLTFileName
Constructors Summary
Methods Summary
protected voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles HTTP GET requests, such as when the user types in a URL into his or her browser or clicks on a hyperlink.

        PersonalData personalData = getPersonalData(request);
        // the third parameter, 'false', indicates that error
        // messages should not be displayed when showing the page.
        showPage(response, personalData, false, this.editXSLTFileName);
    
protected voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Handles HTTP POST requests, such as when the user clicks on a Submit button to update his or her personal data.


        // locate the personal data object and update it with
        // the information the user just submitted.
        PersonalData pd = getPersonalData(request);
        pd.setFirstName(request.getParameter("firstName"));
        pd.setLastName(request.getParameter("lastName"));
        pd.setDaytimePhone(request.getParameter("daytimePhone"));
        pd.setEveningPhone(request.getParameter("eveningPhone"));
        pd.setEmail(request.getParameter("email"));

        if (!pd.isValid()) {
            // show the 'Edit' page with an error message
            showPage(response, pd, true, this.editXSLTFileName);
        } else {
            // show a confirmation page
            showPage(response, pd, false, this.thanksXSLTFileName);
        }
    
private PersonalDatagetPersonalData(javax.servlet.http.HttpServletRequest request)
A helper method that retrieves the PersonalData object from the HttpSession.

        HttpSession session = request.getSession(true);
        PersonalData pd = (PersonalData) session.getAttribute(
                "chap6.PersonalData");
        if (pd == null) {
            pd = new PersonalData();
            session.setAttribute("chap6.PersonalData", pd);
        }
        return pd;
    
public voidinit()
One-time initialization of this Servlet.


              
         
        this.editXSLTFileName = getServletContext().getRealPath(
                "/WEB-INF/xslt/editPersonalData.xslt");
        this.thanksXSLTFileName = getServletContext().getRealPath(
                "/WEB-INF/xslt/confirmPersonalData.xslt");
    
private voidshowErrorPage(javax.servlet.http.HttpServletResponse response, java.lang.Throwable throwable)
If any exceptions occur, this method can be called to display the stack trace in the browser window.

        PrintWriter pw = response.getWriter();
        pw.println("<html><body><h1>An Error Has Occurred</h1><pre>");
        throwable.printStackTrace(pw);
        pw.println("</pre></body></html>");
    
private voidshowPage(javax.servlet.http.HttpServletResponse response, PersonalData personalData, boolean includeErrors, java.lang.String xsltFileName)
A helper method that sends the personal data to the client browser as HTML. It does this by applying an XSLT stylesheet to the DOM tree.

        try {
            org.w3c.dom.Document domDoc =
                    this.personalDataXML.produceDOMDocument(
                    personalData, includeErrors);

            Transformer trans =
                    StylesheetCache.newTransformer(xsltFileName);

            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();

            trans.transform(new DOMSource(domDoc), new StreamResult(writer));
        } catch (Exception ex) {
            showErrorPage(response, ex);
        }