PersonalDataServletpublic class PersonalDataServlet extends HttpServlet A demonstration Servlet that produces two pages. In the first page,
the user is prompted to enter "personal information", including
name, phone number, and Email. In the second page, a summary of this
information is displayed. XSLT is used for all HTML rendering,
so this Servlet does not enforce any particular look and feel. |
Fields Summary |
---|
private PersonalDataXML | personalDataXML | private Templates | editTemplates | private Templates | thanksTemplates |
Methods Summary |
---|
protected void | doGet(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.editTemplates);
| protected void | doPost(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.editTemplates);
} else {
// show a confirmation page
showPage(response, pd, false, this.thanksTemplates);
}
| private PersonalData | getPersonalData(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 void | init()One-time initialization of this Servlet.
TransformerFactory transFact = TransformerFactory.newInstance();
String curName = null;
try {
curName = "/WEB-INF/xslt/editPersonalData.xslt";
URL xsltURL = getServletContext().getResource(curName);
String xsltSystemID = xsltURL.toExternalForm();
this.editTemplates = transFact.newTemplates(
new StreamSource(xsltSystemID));
curName = "/WEB-INF/xslt/confirmPersonalData.xslt";
xsltURL = getServletContext().getResource(curName);
xsltSystemID = xsltURL.toExternalForm();
this.thanksTemplates = transFact.newTemplates(
new StreamSource(xsltSystemID));
} catch (TransformerConfigurationException tce) {
log("Unable to compile stylesheet", tce);
throw new UnavailableException("Unable to compile stylesheet");
} catch (MalformedURLException mue) {
log("Unable to locate XSLT file: " + curName);
throw new UnavailableException(
"Unable to locate XSLT file: " + curName);
}
| private void | showErrorPage(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 void | showPage(javax.servlet.http.HttpServletResponse response, PersonalData personalData, boolean includeErrors, javax.xml.transform.Templates stylesheet)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 = stylesheet.newTransformer();
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
trans.transform(new DOMSource(domDoc), new StreamResult(writer));
} catch (Exception ex) {
showErrorPage(response, ex);
}
|
|