FileDocCategorySizeDatePackage
MailboxServlet.javaAPI DocExample4414Sun Sep 02 14:59:02 BST 2001chap8

MailboxServlet

public class MailboxServlet extends HttpServlet
Demonstrates how to perform session tracking without cookies.

Fields Summary
private String
xsltFileName
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
Display the mailbox.

        try {
            // retrieve current settings from the session
            HttpSession session = req.getSession(true);
            Mailbox mbox = (Mailbox) session.getAttribute("mailbox");
            String curFolderName =
                    (String) session.getAttribute("curFolderName");
            String curSortColumn =
                    (String) session.getAttribute("curSortColumn");

            // produce the DOM tree
            Document doc = MailboxDOMProducer.createMailboxDocument(
                    mbox, curFolderName);

            // prepare the XSLT transformation
            Transformer trans = StylesheetCache.newTransformer(
                    this.xsltFileName);

            // tell the XSLT what column to sort on
            if (curSortColumn != null) {
                trans.setParameter("global.curSortColumn", curSortColumn);
            }

            // allow cookieless session tracking
            if (!req.isRequestedSessionIdFromCookie()) {
                String sessionID = session.getId();
                trans.setParameter("global.sessionID", 
                        ";jsessionid=" + sessionID);
            }

            // send the web page back to the user
            res.setContentType("text/html");
            trans.transform(new javax.xml.transform.dom.DOMSource(doc),
                    new StreamResult(res.getWriter()));

        } catch (ParserConfigurationException pce) {
            throw new ServletException(pce);
        } catch (TransformerConfigurationException tce) {
            throw new ServletException(tce);
        } catch (TransformerException te) {
            throw new ServletException(te);
        }
    
public voiddoPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
Allow users to POST changes to mailbox settings.


        // locate the session for the current user
        HttpSession session = req.getSession(true);

        // if the client changed the current folder, store the new
        // value in the session
        String curFolderName = req.getParameter("changeCurrentFolder");
        if (curFolderName != null) {
            session.setAttribute("curFolderName", curFolderName);
        }

        // if the client changed the sort column, store the new value
        // in the session. The parameter naming convention
        // is: "sortOn[column]". For example, "sortOnfrom" or "sortOnto".
        Enumeration paramNames = req.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String curName = (String) paramNames.nextElement();
            if (curName.startsWith("sortOn")) {
                session.setAttribute("curSortColumn",
                        curName.substring("sortOn".length()));
                break;
            }
        }

        // see if the user submitted a new username
        String username = req.getParameter("username");
        if (username != null) {
            // store the new mailbox in the session
            Mailbox mbox = MailboxFactory.getMailbox(username);
            session.setAttribute("mailbox", mbox);
        }

        // utilize the doGet method to show the page
        doGet(req, res);
    
public voidinit()
Locate the XSLT stylesheet.

        this.xsltFileName = getServletContext().getRealPath(
                "/WEB-INF/xslt/mailbox.xslt");
        if (!new File(this.xsltFileName).exists()) {
            throw new UnavailableException("XSLT stylesheet not found: "
                    + this.xsltFileName, 30);
        }