Download Java And XSLT

Transcript
Unlike other request handlers in this application, PostMsgReqHandler also has a doPost( )
method. The doGet( ) method is responsible for returning a renderer that displays the XHTML
form, while the doPost( ) method is responsible for processing the form submission. Because
the XHTML form contains several required fields and buttons, the doPost( ) method is far more
complex than doGet( ). As the code reveals, almost all of this complexity is introduced because
of error checking and validation logic.
The doPost( ) method checks for illegal/impossible parameters first, returning an error page if
any problems occur. Next, it checks to see what the user typed in. If the user left a required field
blank, the parameter value will be an empty string rather than null. Of course, leading and
trailing spaces must be trimmed in case the user hit the space bar:
msgSubject = msgSubject.trim( );
authorEmail = authorEmail.trim( );
msgText = msgText.trim( );
If any of these fields are empty, the PostMsgRenderer is returned with form field values prefilled:
return new PostMsgRenderer(board, inResponseToMsg,
true, msgSubject, authorEmail, msgText);
This gives the user an opportunity to fill in missing values and try to submit the form again. If all is
well, an instance of ViewMsgRenderer is returned. This allows the user to view the message
that was just submitted.
The source code for PostMsgRenderer is shown in Example 7-38.
Example 7-38. PostMsgRenderer.java
package com.oreilly.forum.servlet;
import
import
import
import
import
import
import
import
com.oreilly.forum.*;
com.oreilly.forum.domain.*;
com.oreilly.forum.xml.*;
java.io.*;
java.util.*;
javax.servlet.*;
javax.servlet.http.*;
org.jdom.*;
/**
* Show the web page that allows a user to post or reply to
* a message.
*/
public class PostMsgRenderer extends Renderer {
private MessageSummary inResponseToMsg;
private BoardSummary board;
private String msgSubject;
private String authorEmail;
private String msgText;
private boolean showError;
/**
* This constructor indicates that the user is replying to an
* existing message.
*/
public PostMsgRenderer(Message inResponseToMsg) {
this.board = inResponseToMsg.getBoard( );