📄 bankaccount.jsp
字号:
<!-- Copyright (c) 2002 Sams Publishing. Weblogic Server 8.1 Unleashed, All Rights Reserved.-->
<STYLE TYPE="text/css">
<!--
table {font-family: Arial, Helvetica, sans-serif; font-size: 10pt; font-style: normal; line-height: normal; font-weight: normal; font-variant: normal; text-transform: none; color: #666666}
.standardtextwhite {font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal; color: #FFFFFF}
.standardtextsmall {font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: normal;}
-->
</STYLE>
<%@ page import="com.weblogicunleashed.account.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%! private clsAccount account; %>
<% // Create an Account COM Component and keep a handle to it in the session.
account = (clsAccount)session.getAttribute("objAccount");
if ( account == null )
account = new clsAccount();
// Keep track of all the user messages during the session.
Vector messages = (Vector)session.getAttribute("messages");
if ( messages == null )
messages = new Vector();
// Get form variables.
String accountName = request.getParameter("accountName");
boolean NEW_ACCOUNT = true;
if (accountName == null)
accountName = "";
else
NEW_ACCOUNT = accountExists(accountName);
double accountBalance = 0;
String amount = request.getParameter("amount");
if ( amount == null || !isNumeric(amount) )
amount = "";
// Find out which button was clicked and take appropriate action.
Enumeration paramNames = request.getParameterNames();
while ( paramNames.hasMoreElements() ){
String paramName = (String)paramNames.nextElement();
if ( paramName.equals("create") ){
if ( accountName != null && !accountName.equals("") && amount != null
&& !amount.equals("") ){
createAccount(accountName, amount);
accountBalance = getAccountBalance(accountName);
messages.add("Account created for '"+accountName+"' with balance of $"
+amount);
}else{
messages.add("Please enter a valid account name and amount to create.");
}
}else{
if ( paramName.equals("deposit") ){
if ( accountName != null && !accountName.equals("")
&& amount != null && !amount.equals("") && !NEW_ACCOUNT){
makeDeposit(accountName, amount);
accountBalance = getAccountBalance(accountName);
messages.add("$" +amount + " deposited into account "
+accountName + ".");
}else{
messages.add("Please enter a valid account name"
+ " and amount for your deposit.");
}
}else{
if ( paramName.equals("withdraw") ){
if ( accountName != null && !accountName.equals("")
&& amount != null && !amount.equals("") && !NEW_ACCOUNT){
accountBalance = getAccountBalance(accountName);
if ( (accountBalance - Double.parseDouble(amount)) < 0){
messages.add("Unable to withdraw the specified amount.");
}else{
makeWithdrawal(accountName, amount);
accountBalance = getAccountBalance(accountName);
messages.add("$" +amount + " withdrawn from account "
+accountName + ".");
}
}else{
messages.add("Please enter a valid account name and amount for "
+ "your withdrawal.");
}
}else{
if (paramName.equals("remove")){
if (accountName != null && !accountName.equals("") && !NEW_ACCOUNT){
removeAccount(accountName);
messages.add("The account '" +accountName + "' has been removed.");
}else{
messages.add("Enter a valid account name to remove.");
}
}
}
}
}
}
// Get the account information after all the updates from current request as
// stored within the COM Component
String[] accountNames = (String[])account.getAccountNames();
double[] accountBalances = (double[])account.getAccountBalances();
session.setAttribute("objAccount",account);
session.setAttribute("messages",messages);
%>
<html>
<head>
<title>Bank Account Interface</title>
</head>
<body bgcolor="#ffffff" link="#3366cc" vlink="#9999cc" alink="#0000cc">
<form name="custsearch" method="post" action="http://localhost:7001/examplesWebApp/BankAccount.jsp">
<table cellpadding="0" cellspacing="0" border="0" width="450" valign="top" bgcolor="#ffffcc" marginwidth="0" marginheight="0" >
<tr>
<td colspan="8" height="20" bgcolor="#0000ff" class="standardtextwhite"><b> Bank Account Interface</b></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="10"><img src="/images/transparent.gif" height="10"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="100">Account Name:</td>
<td><input type="text" name="accountName" size="20" maxlength="20" value="<%=accountName%>"></td>
<td><input type="submit" name="deposit" value="Deposit "></td>
<td><input type="submit" name="create" value="Create"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="10"><img src="/images/transparent.gif" height="10"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="100">Balance:</td>
<td><input type="text" name="balance" size="20" maxlength="20" value="<%=formatCurrency(accountBalance)%>"></td>
<td><input type="submit" name="withdraw" value="Withdraw"></td>
<td><input type="submit" name="remove" value="Remove"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="10"><img src="/images/transparent.gif" height="10"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="100">Amount:</td>
<td colspan="3"><input type="text" name="amount" size="20" maxlength="20" value="<%=amount%>"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="10"><img src="/images/transparent.gif" height="10"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td colspan="4">
<table cellpadding="0" cellspacing="0" border="0" width="100%" align="top">
<tr bgcolor="#0000ff">
<td width="150" class="standardtextwhite"><b> Account Name</b></td>
<td width="50" class="standardtextwhite"><b>Balance</b></td>
<td width="100" class="standardtextwhite"><b>Type</b></td>
</tr>
</table>
</td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td colspan="4">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<% String accountNameDisplay;
double accountBalanceDisplay;
String bgColor = "#ffffff";
if ( accountNames != null ){
for ( int i=0; i < accountNames.length; i++){
accountNameDisplay = accountNames[i];
accountBalanceDisplay = accountBalances[i];
if ( accountNameDisplay != null && !accountNameDisplay.equals("") ){
bgColor = bgColor.equals("#ffffff") ? "#dfdfdf" : "#ffffff";
%>
<tr bgcolor="<%=bgColor%>">
<td height="3" colspan="3"><img src="/images/transparent.gif" height="3"></td>
</tr>
<tr bgcolor="<%=bgColor%>">
<td width="150" class="standardtextsmall"> <%=accountNameDisplay%></td>
<td width="50"class="standardtextsmall"><%=formatCurrency(accountBalanceDisplay)%></td>
<td width="100" class="standardtextsmall">Savings</td>
</tr>
<% }
}
}else{
%>
<tr bgcolor="<%=bgColor%>">
<td height="3" colspan="3"><img src="/images/transparent.gif" height="3"></td>
</tr>
<% }%>
</table>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="10"><img src="/images/transparent.gif" height="10"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
<tr>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td colspan="4">
<% bgColor = "#ffffff"; %>
<table cellpadding="0" cellspacing="0" border="1" width="100%">
<tr>
<td>
<div style='width:100%; height:25; background-color:white; overflow:auto'>
<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="#ccffcc">
<tr>
<td class="standardtextsmall">
<%for (ListIterator i= messages.listIterator(messages.size()); i.hasPrevious();){ %>
<%= ((String) i.previous())%><br>
<%}%>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
<td width="10"><img src="/images/transparent.gif" width="10"></td>
<td width="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
<td colspan="6" height="20"><img src="/images/transparent.gif" height="20"></td>
<td width ="2" bgcolor="#0000ff"><spacer type="block" width="2"></td>
</tr>
</tr>
<td colspan="8" width="335" height"2" bgcolor="#0000ff"><img src="/images/transparent.gif" width="425" height="2"></td>
</tr>
</table>
</form>
<span class="standardtextsmall">Copyright 2002, Sams Publishing, Inc. Weblogic 7.0 Unleashed. All rights reserved.</span>
</body>
</html>
<%!
private void createAccount(String name, String amount) throws com.bea.jcom.AutomationException, java.io.IOException {
String[] accountName = new String [1];
double[] amountArray = new double[1];
accountName[0] = name;
amountArray[0] = Double.parseDouble(amount);
account.create(accountName, amountArray);
}
private void removeAccount(String name) throws com.bea.jcom.AutomationException, java.io.IOException {
String[] accountName = new String [1];
accountName[0] = name;
account.remove(accountName);
}
private void makeDeposit(String name, String amount) throws com.bea.jcom.AutomationException, java.io.IOException {
String[] accountName = new String [1];
double[] amountArray = new double[1];
accountName[0] = name;
amountArray[0] = Double.parseDouble(amount);
account.deposit(accountName, amountArray);
}
private void makeWithdrawal(String name, String amount) throws com.bea.jcom.AutomationException, java.io.IOException {
String[] accountName = new String [1];
double[] amountArray = new double[1];
accountName[0] = name;
amountArray[0] = Double.parseDouble(amount);
account.withdrawal(accountName, amountArray);
}
private double getAccountBalance(String name) throws com.bea.jcom.AutomationException, java.io.IOException {
String[] accountName = new String [1];
accountName[0] = name;
return account.getAccountBalance(accountName);
}
private boolean accountExists(String accountName)throws com.bea.jcom.AutomationException, java.io.IOException {
boolean NEW_ACCOUNT = true;
String[] accountNamesLookUp = (String[])account.getAccountNames();
if ( accountNamesLookUp != null ){
for ( int i=0; i < accountNamesLookUp.length; i++){
String accountNameLookUp = accountNamesLookUp[i];
if ( accountNameLookUp != null && accountNameLookUp.equals(accountName) ){
NEW_ACCOUNT = false;
}
}
}
return NEW_ACCOUNT;
}
private static boolean isNumeric(String amount) {
amount = amount.trim();
try {
if (Double.parseDouble(amount) > 0) {
return true;
}else{
return false;
}
}catch (Exception ex){
return false;
}
}
private String formatCurrency(double amount) {
return NumberFormat.getCurrencyInstance(java.util.Locale.US).format(amount);
}
%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -