AccountServlet.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- 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");
- }
- }
- }