实例功能内容:
 
模拟账户金额查询(无数据库操作)
 
1、账户查询类:Account.java
 
package test;

public class Account {
        public String accountnum = "";
        public String accountname = "";
        public double blances = 0;
        public Account()
        {
                
        }
        public Account(String accountnum,String accountname,double blances)
        {
                this.accountnum = accountnum;
                this.accountname = accountname;
                this.blances = blances;
        }
        protected static Account firstAccount = new Account("5968874733271","dragon",3000.0);
        protected static Account secondAccount = new Account("563524241521","lucy",7000.0);
        protected static Account thirdAccount = new Account("8736252451432","lily",97000.0);
        public static Account getAccountInfo(String accountnum)
        {
                if( accountnum == null || accountnum.equals(""))
                {
                        return null;
                }
                else if(firstAccount.getAccountNum().equals(accountnum))
                {
                        return firstAccount;
                }
                else if(secondAccount.getAccountNum().equals(accountnum))
                {
                        return secondAccount;
                }
                else if(thirdAccount.getAccountNum().equals(accountnum))
                {
                        return thirdAccount;
                }
                else    
                {
                        return null;
                }
        }
        
        public String getAccountNum()
        {
                return accountnum;
        }
        
        public String getName()
        {
                return accountname;
        }

        public double getBalance()
        {
                return blances;
        }
}
 
2、myServlet.getAccount
 
package myServlet;

import test.Account;
import java.io.*;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class getAccount 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, IOException {            
                
             String AccountNo = (String)request.getParameter("AccountNo");
             Account oneAccount = (Account)Account.getAccountInfo(AccountNo);
                
             if(oneAccount == null)
                {    
                             RequestDispatcher rdc1 = request.getRequestDispatcher("../test.html");
                             rdc1.forward(request, response);    
                             return;
             }
                request.setAttribute("Account", oneAccount);
                RequestDispatcher rdc2 = request.getRequestDispatcher("../showBalance.jsp");
                rdc2.forward(request, response);
        }    

        // <editor-fold defaultstate="collapsed" desc="HttpServlet 方法。单击左侧的 + 号以编辑代码。">
        /**    
        * Handles the HTTP <code>GET</code> method.
        * @param request servlet request
        * @param response servlet response
        */

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, 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, IOException {
             processRequest(request, response);
        }

        /**    
        * Returns a short description of the servlet.
        */

        public String getServletInfo() {
                return "Short description";
        }// </editor-fold>    
}
 
用户界面:
 
1、check.html
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
            <form action="servlet/getAccount" method="post">
                    账户编号:<input type="text" name="AccountNo" id="AccountNo" maxlength="20" size="20"/><br>
                    <input type="submit" value="查询" name="submit" size="20"/>
            </form>
    </body>
</html>
 
2、showBalances.jsp
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="test.Account" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">

<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>JSP Page</title>
        </head>
        <body>
                <jsp:useBean id="acount" class="test.Account" scope="request"/>
                <%
                     Account oneAccount = (Account)request.getAttribute("Account");
                %>
                您好,<%=oneAccount.getName()%><br/>
                您的账户:<%=oneAccount.getAccountNum()%><br/>
                您的余额:<%=oneAccount.getBalance()%><br/>
        </body>
</html>
 
配置文件(Web.Config)
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]">
        <distributable/>
        <servlet>
                <servlet-name>getAccount</servlet-name>
                <servlet-class>myServlet.getAccount</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>getAccount</servlet-name>
                <url-pattern>/servlet/getAccount</url-pattern>
        </servlet-mapping>    
        <session-config>
                <session-timeout>
                        30
                </session-timeout>
        </session-config>
        <welcome-file-list>
                <welcome-file>check.html</welcome-file>
                </welcome-file-list>
        </web-app>
 
     实例说明
 
     此实例为书上的例子,但有所改动。书上的例子在showBalance.jsp页面中采用UseBean标签来调用Account.java类,试了N久,该方式根本获取不了值;在getAccount.java中修改请求设置属性值,改为
request.setAttribute("Account", oneAccount);
 
书中的此段代码为
request.setAttribute("AccountNum",AccountNum);