Java Reference
In-Depth Information
String subject = request.getParameter ("subject");
String author = request.getParameter ("author");
String postText = request.getParameter ("postText");
String parent = request.getParameter ("parent");
AddPostCommand addPost = new AddPostCommand();
try {
addPost.setBoard(board);
addPost.setSubject(subject);
addPost.setAuthor(author);
addPost.setPostText(postText);
addPost.setParent(parent);
addPost.initialize();
We are putting our add and initialize methods into their own try/catch
loop. This design will allow us to catch the exceptions that we raised for vali-
dation failures. In a more robust architecture, we'd create a bean from the
data in our custom exception and return an appropriate error JSP .
} catch (Throwable Exception) {
try {
ServletContext servletContext = getServletContext ();
RequestDispatcher dispatcher =
servletContext.getRequestDispatcher("/JSP/AddPostError.jsp");
dispatcher.forward(request, response);
return;
} catch (Throwable exception) {
exception.printStackTrace();
}
}
addPost.execute();
request.setAttribute("AddPostCommand", addPost);
ServletContext servletContext = getServletContext ();
RequestDispatcher dispatcher =
servletContext.getRequestDispatcher("/servlet/
bbs.ShowBoardController");
dispatcher.forward(request, response);
In this case, no dynamic data is to be displayed beyond success or failure. For
the success case, users would probably prefer to continue browsing the board
from the ShowBoard view. Therefore, through our controller, we simply dis-
patch the user to ShowBoardController . Our request object already has the
requisite board parameter set, so there is no need to re-create one.
} catch (Throwable theException) {
try {
java.io.PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Post List Controller</TITLE></HEAD>");
Search WWH ::




Custom Search