Chapter 3
Chapter 5
Chapter 7
Chapter 8
Chapter 9
Download All Code
Home
 
Chapter 7 Examples
7.1 to 7.12   7.13 to 7.24   7.25 to 7.35

Download all Chapter 7 code

View code examples 7.25 to 7.35
7.25   7.26   7.27   7.28   7.29   7.30
7.31   7.32   7.33   7.34   7.35


Example 7.25 Composite View with Runtime Content Inclusion

<table border=1 valign="top" cellpadding="2%" 
  width="100%">
    <tr>
        <td><jsp:include page="news/worldnews.jsp" 
            flush="true"/> </td>
    </tr>
    <tr>
        <td><jsp:include page="news/countrynews.jsp" 
            flush="true"/> </td>
    </tr>
    <tr>
        <td><jsp:include page="news/customnews.jsp" 
            flush="true"/> </td>
    </tr>
    <tr>
        <td><jsp:include page="news/astronomy.jsp" 
            flush="true"/> </td>
    </tr>
</table>

Example 7.26 A Region and Sections

<region:render template='portal.jsp'>
  <region:put section='banner' content = 'banner.jsp' />
  <region:put section = 'controlpanel' content = 
      'ProfilePane.jsp' />
  <region:put section='mainpanel' content = 
      'mainpanel.jsp' />
  <region:put section='footer' content='footer.jsp' />
</region:render>

Example 7.27 Template Definition

<region:render section='banner'/>
<table width="100%">
    <tr align="left" valign="middle">
        <td width="20%">
      <!-- menu region -->
      <region:render section='controlpanel' />
        </td>
        <td width="70%" align="center">
      <!-- contents -->
      <region:render section='mainpanel' />
        </td>
    </tr>
</table>

Example 7.28 Section Subview – banner.jsp

<table width="100%" bgcolor="#C0C0C0">
<tr align="left" valign="middle">
  <td width="100%">

  <TABLE ALIGN="left" BORDER=1 WIDTH="100%">
  <TR ALIGN="left" VALIGN="middle">
    <TD>Logo</TD>
    <TD><center>Sun Java Center</TD>
  </TR>
  </TABLE>

  </td>
</tr>
</table>

Example 7.29 Controller Servlet with Command and Controller Strategy

public class Controller extends HttpServlet {
  /** Processes requests for both HTTP  
   * <code>GET</code> and <code>POST</code> methods.
   * @param request servlet request
   * @param response servlet response
   */
  protected void processRequest(HttpServletRequest 
    request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    String next;

    try {
      // Log pattern info
      LogManager.recordStrategy(request, 				
        "Service To Worker", 
	      " ServletFront Strategy;" + 
      " JSPView Strategy; JavaBean helper Strategy");

      LogManager.logMessage(request, getSignature(), 
        "Process incoming request. ");

      // Use a helper object to gather parameter 
      // specific information.
      RequestHelper helper = new 
        RequestHelper(request, response);

      LogManager.logMessage(request, getSignature(), 
          "Getting command object helper");

      // Get command object helper
      Command command = helper.getCommand();
      // delegate processing to the command object, 
      // passing request and response objects along
      next = command.execute(helper);

      /** If the above command returns a value, we 
        * will dispatch from the controller. In this 
        * example, though, the command will use a 
        * separate dispatcher component to choose a 
        * view and dispatch to that view. The command 
        * object delegates to this dispatcher 
        * component in its execute method, above, and 
        * control should not return to this point **/
    }
    catch (Exception e) {
      LogManager.logMessage( 
        "EmployeeController(CommandStrategy)",
        e.getMessage() );

      /** ApplicationResources provides a simple API 
        * for retrieving constants and other 
        * preconfigured values**/
      next = ApplicationResources.getInstance(). 
                    getErrorPage(e);
    }

    dispatch(request, response, next);

  }

  /** Handles the HTTP <code>GET</code> method.
   * @param request servlet request
   * @param response servlet response
   */
  protected void doGet(HttpServletRequest request, 
    HttpServletResponse response)
    throws ServletException, java.io.IOException {
      processRequest(request, response);
  }

  /** Handles the HTTP <code>POST</code> method.
   * @param request servlet request
   * @param response servlet response
   */
  protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
  throws ServletException, java.io.IOException {
    processRequest(request, response);
  }

  /** Returns a short description of the servlet. */
  public String getServletInfo() {
    return getSignature();
  }

  /** dispatcher method */
  protected void dispatch(HttpServletRequest request, 
    HttpServletResponse response,
    String page) throws 
  javax.servlet.ServletException, 
    java.io.IOException {
      RequestDispatcher dispatcher = 
       getServletContext().getRequestDispatcher(page);
      dispatcher.forward(request, response);
  }

  public void init(ServletConfig config) throws 
      ServletException {
    super.init(config);
  }

  public void destroy() { }

  private String getSignature() {
    return "ServiceToWorker-Controller";
  }
}

Example 7.30 Command Interface

public interface Command {

    public String execute(RequestHelper helper) throws
  javax.servlet.ServletException, java.io.IOException;
}

Example 7.31 ViewAccountDetailsCommand

public class ViewAccountDetailsCommand implements 
  Command {
  public ViewAccountDetailsCommand() { }

  // view account details operation
  public String execute(RequestHelper helper)
    throws javax.servlet.ServletException, 
  java.io.IOException {
    /** This will tell the user that a system error 
      * has occured and will typically not be seen. It 
      * should be stored in a resource file **/
    String systemerror = 
      "/jspdefaultprocessingerror.jsp";
        
    LogManager.logMessage(helper.getRequest(), 
      "ViewAccountDetailsCommand", 
      "Get Account Details from an adapter object");

    /** Use an adapter to retrieve data from business 
      * service, and store it in a request attribute.
      * Note: Object creation could be avoided via 
      * factory, but for example purposes object 
      * instantiation is shown **/
    AccountingAdapter adapter = new 
                           AccountingAdapter();
    adapter.setAccountInfo(helper);

    LogManager.logMessage(helper.getRequest(), 
  "ViewAccountDetailsCommand", "processing complete");

    /** Note: Object creation could be avoided via 
      * factory, but for example purposes object 
      * instantiation is shown**/
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.dispatch(helper);
   
    /** This return string will not be sent in a 
      * normal execution of this scenario, because 
      * control is forwarded to another resource 
      * before reaching  this point. Some commands do 
      * return a String,  though, so the return value 
      * is included for  correctness. **/
    return systemerror;
  }
}

Example 7.32 AccountingAdapter

public class AccountingAdapter {
    public void setAccountInfo(
      RequestHelper requestHelper) {
        LogManager.logMessage(
          requestHelper.getRequest(), 
          "Retrieving data from business tier");

        // retrieve data from business tier via 
        // delegate. Omit try/catch block for brevity.
        AccountDelegate delegate = 
                new AccountDelegate();
        AccountVO account =	
          delegate.getAccount( 
            requestHelper.getCustomerId(),
            requestHelper.getAccountKey());

        LogManager.logMessage( 
          requestHelper.getRequest(), 
  "Store account value object in request attribute");

      // transport data using request object					
      requestHelper.getRequest().setAttribute(
         "account", account);
    }
}

Example 7.33 View – accountdetails.jsp

<html>
<head><title>AccountDetails</title></head>
<body>

<jsp:useBean id="account" scope="request"
  class="corepatterns.util.AccountVO" /> 


<h2><center> Account Detail for <jsp:getProperty 
  name="account" property="owner" />
</h2> <br><br>
<table border=3>
<tr>
<td>
Account Number :
</td>
<td>
<jsp:getProperty name "account" property="number" />
</td>
</tr>

<tr>
<td>
Account Type:
</td>
<td>
<jsp:getProperty name="account" property="type" />
</td>
</tr>

<tr>
<td>
Account Balance:
</td>

<td>
<jsp:getProperty name="account" property="balance" />
</td>
</tr>

<tr>
<td>
OverDraft Limit:
</td>
<td>
<jsp:getProperty name="account" 
  property="overdraftLimit" />
</td>
</tr>

</table>


<br>
<br>

</center>
<%@ include file="/jsp/trace.jsp" %>
</body>
</html>

Example 7.34 Dispatcher View Controller Servlet

public class Controller extends HttpServlet {

  /** Processes requests for both HTTP 
    * <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
  protected void processRequest(HttpServletRequest 
    request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
    String nextview;
    try {
      LogManager.recordStrategy(request, 
        "Dispatcher View", 
        " Servlet Front Strategy; " + 
    "JSP View Strategy; Custom tag helper Strategy");
      LogManager.logMessage(request, getSignature(), 
        "Process incoming request. ");

      // Use a helper object to gather parameter 
      // specific information.
      RequestHelper helper = new 
          RequestHelper(request, response);
      LogManager.logMessage(request, 
        getSignature(), " Authenticate user");

      Authenticator auth = new BasicAuthenticator();
      auth.authenticate(helper);

      //This is an oversimplification for the sake of 
      // simplicity. Typically, there will be a 
      // mapping from logical name to resource name at 
      // this point
      LogManager.logMessage(request, getSignature(), 
        "Getting nextview");
      nextview = request.getParameter("nextview");

      LogManager.logMessage(request, getSignature(), 
        "Dispatching to view: " + nextview);
    }
    catch (Exception e) {
      LogManager.logMessage(
        "Handle exception appropriately", 
        e.getMessage() );
      /** ApplicationResources provides a simple API 
        * for retrieving constants and other 
        * preconfigured values**/
      nextview = ApplicationResources.getInstance().
          getErrorPage(e);
    }
    dispatch(request, response, nextview);
  }

  /** Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
  protected void doGet(HttpServletRequest request, 
    HttpServletResponse response)
  throws ServletException, java.io.IOException {
    processRequest(request, response);
 }

  /** Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
  protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
  throws ServletException, java.io.IOException {
    processRequest(request, response);
  }

  /** Returns a short description of the servlet. */
  public String getServletInfo(){
      return getSignature();
  }

  public void init(ServletConfig config) throws 
    ServletException {
    super.init(config);
  }

  public void destroy() { }

  /**
    * dispatcher method
    */
  protected void dispatch(HttpServletRequest request,
    HttpServletResponse response, String page) 
    throws javax.servlet.ServletException, 
    java.io.IOException {
        RequestDispatcher dispatcher = 
          getServletContext(). 
            getRequestDispatcher(page);
        dispatcher.forward(request, response);
  }

  private String getSignature()  {
    return "DispatcherView-Controller"; 
  }
}

Example 7.35 View – accountdetails.jsp

<%@ taglib uri="/web-INF/corepatternstaglibrary.tld"
  prefix="corepatterns" %>

<html>
<head><title>AccountDetails</title></head>
<body>

<corepatterns:AccountQuery 
  queryParams="custid,acctkey" scope="request" />

<h2><center> Account Detail for <corepatterns:Account
  attribute="owner" /></h2> <br><br>

<tr>
  <td>Account Number :</td>
  <td><corepatterns:Account attribute="number" /></td>
</tr>

<tr>
  <td>Account Type:</td>
  <td><corepatterns:Account attribute="type" /></td>
</tr>

<tr>
  <td>Account Balance:</td>
  <td><corepatterns:Account attribute="balance" /></td>
</tr>

<tr>
 <td>OverDraft Limit:</td>
 <td><corepatterns:Account attribute="overdraftLimit" /></td>
</tr>
<table border=3>
</table>
</corepatterns:AccountQuery>

<br>
<br>

</center>
<%@ include file="/jsp/trace.jsp" %>
</body>
</html>
Ex. 7.1 to 7.12 | Ex 7.13 to 7.24 | Ex. 7.25 to 7.35