accountservlet.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 79 行

JAVA
79
字号
package bible.jsp;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Title:        AccountServlet
 * Description:  Loads an account from the database
 * Copyright:    Copyright (c) 2001
 * Company:      ZeeWare Inc.
 * @author Gary Wells
 * @version 1.0
 */

public class AccountServlet extends HttpServlet {

  // private properties
  private AccountBean account;
  private String accountNumber;

  /**
   * Processes the request and forwards onto the next JSP.
   * @param     HttpServletRequest
   * @param     HttpServletResponse
   * @exception ServletException
   * @exception IOException
   * @see       javax.servlet.http.HttpServlet#service()
   */
  public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    // get the account number from the request
    accountNumber = request.getParameter("accountNumber");

    // create the band, set its properties, and scope it to the request
    account = new AccountBean();
    loadAccount();
    request.setAttribute("account", account);


    // determine next page based upon cmd
    String nextPage = "AccountSearch.jsp";
    if ("edit".equals(request.getParameter("cmd"))) {
      nextPage = "/AccountEdit.jsp";
    } else if ("detail".equals(request.getParameter("cmd"))) {
      nextPage = "/AccountDetail.jsp";
    }

    // forward the request to the next page
    try {
      RequestDispatcher rd = getServletContext().getNamedDispatcher(nextPage);
      rd.forward(request, response);
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  private void loadAccount() {
    // connect to the database here and load bean from account data, however
    // this example with set the static data baase upon the request parameter

    if (accountNumber.equals("110-12345")) {
      account.setAccountNumber("110-12345");
      account.setAccountName("Martin Moneybags");
      account.setStatus("A");

    } else if (accountNumber.equals("110-98765")) {
      account.setAccountNumber("110-98765");
      account.setAccountName("Lois Lottamoney");
      account.setStatus("A");

    } else if (accountNumber.equals("110-13579")) {
      account.setAccountNumber("110-13579");
      account.setAccountName("Greg Goinbroke");
      account.setStatus("A");
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?