本例子是參考了一些網站上有關 JSP 對 XML 的操作的相關文檔,又結合了一些個人的體會。例子涉及的內容是,開發的一個企業內部定餐系統后臺管理端的部分代碼,功能主要集中在對于餐館基本信息的管理。
該例子本身開發的起因是我在原公司和同事們一個玩笑的一部分。特此也表達對那些一起共事的朋友們的想念。
例子本身是在 TOMCAT4.01 平臺下運行的 B/S 結構的程式。有關 TOMCAT 的配置,這里不做說明。只講解一下相關文件及文件夾的目錄結構。
目錄結構說明:
/tomcat/webapps/canyin/ ----- 主目錄
/tomcat/webapps/canyin/jsp/ -----JSP 文件目錄
/tomcat/webapps/canyin/jsp/admin/ ----- 實現后臺管理的 JSP 文件的存放目錄
/tomcat/webapps/canyin/WEB-INF/classes/canyin/ ------javabean 文件的存放目錄
/tomcat/webapps/canyin/data/ -----xml 文件存放目錄
/tomcat/webapps/ROOT/ -----tomcat 啟動文件存放文件夾,只存放了 index.html 文件
文件簡單說明:
/tomcat/webapps/canyin/data/users.xml ----- 記錄用戶信息
/tomcat/webapps/canyin/data/restaurants.xml ----- 記錄餐館的基礎信息
/tomcat/webapps/ROOT/index.html ----- 首頁,頁面出現輸入框,要求用戶輸入用戶名,密碼
/tomcat/webapps/canyin/jsp/loginjudge.jsp ----- 用戶身份判斷頁面,根據用戶名稱和密碼決定頁面是轉入后臺管理端,還是前臺客戶端。本例子中,用戶身份一旦確認為有管理權限,可以進入后臺管理端,就直接跳到餐館基本信息管理頁面,簡化說明的流程。
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp ----- 餐館基本信息管理頁面,管理餐館的名稱,電話,地址等信息
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class ----- 后臺管理端檢測標志用戶身份的 session 的值,如果不是管理員的話,跳回登陸頁面。
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class ----- 連接 xml 文件
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class ----- 寫入 xml 文件
文件詳細介紹及附帶代碼說明。
/tomcat/webapps/canyin/data/users.xml
代碼:
<?xml version="1.0" encoding="UTF-8" ?>
- <users>
<user name="joard" password="joard" roles="admin" />
<user name="joard01" password="joard01" roles="user" />
<user name="joard02" password="joard02" roles="user" />
</users>
說明:字段含義是用戶名,密碼以及用戶的身份
/tomcat/webapps/canyin/data/restaurants.xml
代碼:
<?xml version="1.0" encoding="UTF-8" ?>
- <restaurants num="10">
- <restaurant id="1">
<name> 上海亭快餐店 </name>
<phone>021-76546726</phone>
<address> 百老匯廣場 B 座 </address>
</restaurant>
- <restaurant id="8">
<name> 香格里拉大飯店 </name>
<phone>021-2312134</phone>
<address> 南京路 1023 號 </address>
</restaurant>
</restaurants>
說明: <num> 屬性是記錄在 restaurants.xml 文件中總共有過多少條記錄,每新增一條,無論以后刪除是否,該值都會增加 1 ,就好象數據庫中習慣使用的自動增加 1 的 id 項。用來給新增的 <restaurant> 的屬性 <id> 賦一個唯一的值。其它的字段意思比較明顯。
/tomcat/webapps/ROOT/index.html (單純的 HTML 代碼)
代碼:
<html>
<head>
<title>oddWorld 餐飲系統 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body onload="javascript:dataform.username.focus()">
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="http://www.knowsky.com/images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統登錄 </td>
<td><img src="http://www.knowsky.com/images/top_r2.GIF" width="294" height="22"></td>
</tr>
</table>
<br>
<br>
<table width="300" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td height="200" valign="top" align="center">
<p align="center">
<table width="100%" border="0" cellspacing="1" cellpadding="5" bgcolor=#999999 class=a9px>
<tr>
<td bgcolor="#efefef"> 餐飲系統登錄 </td>
</tr>
<tr>
<td bgcolor="#FFFFFF" valign="top" align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<form name=dataform method=post action=''canyin/jsp/loginjudge.jsp''>
<tr>
<td width="100"><b> 登錄名: </b></td>
<td>
<input maxlength=16
name="username" class=stedit value="joard">
</td>
</tr>
<tr>
<td width="100"><b> 密碼: </b></td>
<td>
<input class=stedit maxlength=16
name="userpass" type=password value="oddworld">
</td>
</tr>
</form>
</table>
<br>
<table border=0 cellpadding=0 cellspacing=0>
<tbody>
<tr>
<td>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value=" 登 錄 ">
</td>
<td> </td>
<td>
<input class=stbtm name=Submit onClick="javascript:window.location.href=''index.asp?myjoke=1'';" type=button value=" 修改密碼 ">
</td>
<td> </td>
</tr>
</tbody>
</table>
<br>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
<script language=javascript>
<!--
function checkform()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform.username.value))
{
alert(" 登錄名不能為空 !");
return false;
}
if (Checkblank.test(dataform.userpass.value))
{
alert(" 密碼不能為空 !");
return false;
}
window.dataform.submit();
}
-->
</script>
說明:把用戶名稱和用戶密碼提交到 /tomcat/webapps/canyin/jsp/loginjudge.jsp
/tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class (代碼是相應的 java 文件)
package canyin;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
public class checkSessionBean {
private boolean bolCheckPass=false;
private HttpServletRequest request = null;
public boolean checkSessionBean(HttpServletRequest request,String strSessionName,String strCheckValue){
public boolean checkSessionBean(HttpServletRequest request){
HttpSession session = request.getSession(false);
return(bolCheckPass);
if (strSessionName==null || strCheckValue==null){
return(bolCheckPass);
}else{
if (session!=null && session.getValue(strSessionName)!=null){
bolCheckPass=session.getValue(strSessionName).equals(strCheckValue);
}
return(bolCheckPass);
}
}
}
說明:檢驗參數傳入的 session 名稱的數值和參數傳入的字段的數值是否相等。
/tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class
代碼:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
public class connXmlBean {
private DocumentBuilderFactory factory=null;
private DocumentBuilder builder=null;
private Document doc=null;
public connXmlBean(){}
public String connXml(String xmlFileName){
String strExc="";
try{
factory = DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
doc=builder.parse(xmlFileName);
doc.normalize();
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
public Document getXmlDoc(){
return(doc);
}
}
說明:打開一個指定 xml 文件
/tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class
代碼:
package canyin;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import org.w3c.dom.*;
public class writeXmlBean {
public writeXmlBean(){}
public String writeXml(Document doc,String xmlFileName){
String strExc="";
try{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(xmlFileName));
transformer.transform(source,result);
}catch(Exception e){
strExc=e.toString();
}
return(strExc);
}
}
說明:寫入 dom 的內容到一個指定的 xml 文件。
/tomcat/webapps/canyin/jsp/loginjudge.jsp
代碼:
<%-- oddWorld 餐飲管理系統 ( 簡體中文版 ) 2002 年 12 月 1 日
copy right by joard ast
loginjudge.jsp 功能:用戶身份校驗,根據 /data/user.xml 文件內標示的用戶不同的身份
決定轉入后臺管理頁面,還是客戶點菜頁面。
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<%
session.setMaxInactiveInterval(1800);
Document doc;
NodeList users;
String strExc="";
String strUsername,strPassword;
strUsername=(String)request.getParameter("username");
strPassword=(String)request.getParameter("userpass");
// 校驗數據是否為空
if (strUsername=="" || strPassword=="" ){
out.println("<script language=''javascript''>");
out.println("alert('' 用戶名或密碼有空值 !'');");
out.println("window.location.href=''/index.html'';");
out.println("</script>");
return;
}
xmlBean.connXml("webapps/canyin/data/users.xml");
doc=xmlBean.getXmlDoc();
try{
users =doc.getElementsByTagName("user");
for (int i=0;i<users.getLength();i++){
Element user=(Element) users.item(i);
String strAtrNameValue=user.getAttributeNode("name").getNodeValue();
String strAtrPassWordValue=user.getAttributeNode("password").getNodeValue();
String strAtrRoleValue=user.getAttributeNode("roles").getNodeValue();
if (strAtrNameValue.equals(strUsername) && strAtrPassWordValue.equals(strPassword)){
if (strAtrRoleValue.equals("admin")){
out.println("<script language=''javascript''>");
out.println("alert('' 歡迎管理員登陸系統 !'');");
out.println("</script>");
// 設置標示用戶身份的 session ( sesUserRole ) ,管理員身份為 admin
session.setAttribute("sesUserRole","admin");
// 跳轉到管理頁面
response.sendRedirect("admin/admin_rest.jsp");
return;
}else{
// 設置標示用戶身份的 session ( sesUserRole ) ,管理員身份為 user
session.setAttribute("sesUserRole","user");
// 跳轉到普通用戶頁面
response.sendRedirect("index.jsp");
return;
}
}else{
out.println("<script language=''javascript''>");
out.println("alert('' 用戶名或密碼錯誤 !'');");
out.println("history.go(-1);");
out.println("</script>");
return;
}
}
}catch(Exception e){
strExc=e.toString();
}
%>
說明: .......
/tomcat/webapps/canyin/jsp/admin/admin_rest.jsp
代碼:
<%-- oddWorld 餐飲管理系統 ( 簡體中文版 ) 2002 年 12 月 1 日
copy right by joard ast
admin_rest.jsp 功能:后臺管理頁面,餐館管理頁面。
--%>
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page language="java" import="javax.xml.parsers.*" %>
<%@ page import="javax.xml.transform.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="canyin.*" %>
<%@ include file="../../include/sys_dialog.jsp" %>
<jsp:useBean id="checkSessionBean" class="canyin.checkSessionBean" scope="page" />
<jsp:useBean id="xmlBean" class="canyin.connXmlBean" scope="page" />
<jsp:useBean id="writeXmlBean" class="canyin.writeXmlBean" scope="page" />
<%// 校驗可戶身份,判斷是不是管理員
if(!checkSessionBean.checkSessionBean(request,"sesUserRole","admin")){
out.print(showDialog(" 您沒有管理的權限 !","/index.html"));
return;
}
// 從餐館資料文件 rest.xml 中得到相關數據
Document doc;
NodeList restaurants;
String strAct;
int intId=0;
String strOperation="show";
// 接受外部傳入的參數
strAct=(String)request.getParameter("act");
xmlBean.connXml("webapps/canyin/data/restaurants.xml");
doc=xmlBean.getXmlDoc();
restaurants =doc.getElementsByTagName("restaurant");
// 根據外部傳入的參數來決定對 restaurant.xml 文件的操作
if (strAct!=null){
if(strAct.equals("addnewDo")){
String strName;
String strPhone;
String strAddress;
Text textseg;
strName=(String)request.getParameter("name").trim();
strPhone=(String)request.getParameter("phone").trim();
strAddress=(String)request.getParameter("address").trim();
// 數據校驗
if(strName==null){
out.print(showDialog(" 餐館名稱不能為空 !"));
return;
}
if(strPhone==null){
out.print(showDialog(" 餐館電話不能為空 !"));
return;
}
/*if(strAddress==null){
out.print(showDialog(" 餐館地址不能為空 !"));
return;
}*/
// 校驗數據的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strName)){
out.print(showDialog(" 餐館名稱重復 !"));
return;
}else{
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone)){
out.print(showDialog(" 餐館電話重復 !"));
return;
}
}
}
// 得到已有的記錄數,給新增的餐館記錄設定唯一的遞增的 id 屬性
int intNum=0;
Element restNum=(Element)doc.getElementsByTagName("restaurants").item(0);
intNum=Integer.parseInt(restNum.getAttributeNode("num").getNodeValue());
intNum+=1;
// 為 restaurants 的屬性 num 的數值加 1
restNum.getAttributeNode("num").setNodeValue(String.valueOf(intNum));
// 新增節點
Element newRestaurant=doc.createElement("restaurant");
Attr newArrId=doc.createAttribute("id");
//Attribute newArrId = new Attribute("id",String.valueOf(intNum));
textseg=doc.createTextNode(String.valueOf(intNum));
newArrId.setValue(String.valueOf(intNum));
newRestaurant.setAttributeNode(newArrId);
Element newName=doc.createElement("name");
textseg=doc.createTextNode(strName);
newName.appendChild(textseg);
newRestaurant.appendChild(newName);
Element newPhone=doc.createElement("phone");
textseg=doc.createTextNode(strPhone);
newPhone.appendChild(textseg);
newRestaurant.appendChild(newPhone);
Element newAddress=doc.createElement("address");
textseg=doc.createTextNode(strAddress);
newAddress.appendChild(textseg);
newRestaurant.appendChild(newAddress);
doc.getDocumentElement().appendChild(newRestaurant);
// 調用 bean 寫入相應的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}
if(strAct.equals("modiDo")){
String strName;
String strPhone;
String strAddress;
Text textseg;
int modiId;
// 記錄要修改的記錄是 item(i) 的哪一項
int intI=0;
strName=(String)request.getParameter("name").trim();
strPhone=(String)request.getParameter("phone").trim();
strAddress=(String)request.getParameter("address").trim();
modiId=Integer.parseInt(request.getParameter("recordId").trim());
// 數據校驗
if(strName==null){
out.print(showDialog(" 餐館名稱不能為空 !"));
return;
}
if(strPhone==null){
out.print(showDialog(" 餐館電話不能為空 !"));
return;
}
if(modiId==0){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}
/*if(strAddress==null){
out.print(showDialog(" 餐館地址不能為空 !"));
return;
}*/
// 標志顯示記錄存在
boolean recordExist=false;
// 校驗數據的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==modiId){
recordExist=true;
intI=i;
}
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strName) && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())!=modiId ){
out.print(showDialog(" 餐館名稱重復 !"));
return;
}else{
if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone) && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())!=modiId ){
out.print(showDialog(" 餐館電話重復 !"));
return;
}
}
}
if(!recordExist){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}else{
// 進行記錄更改的操作
try{
Element modiRestaurant=(Element) restaurants.item(intI);
modiRestaurant.getElementsByTagName("name").item(0).getFirstChild().setNodeValue(strName);
modiRestaurant.getElementsByTagName("phone").item(0).getFirstChild().setNodeValue(strPhone);
modiRestaurant.getElementsByTagName("address").item(0).getFirstChild().setNodeValue(strAddress);
// 調用 bean 寫入相應的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}catch(Exception e){}
}
}
// 進行刪除操作
if(strAct.equals("del")){
int delId;
// 記錄要修改的記錄是 item(i) 的哪一項
int intI=0;
delId=Integer.parseInt(request.getParameter("recordId").trim());
if(delId==0){
out.print(showDialog(" 你要修改餐館的記錄不存在 !"));
return;
}
file:// 標志顯示記錄存在
boolean recordExist=false;
// 校驗數據的唯一性
for(int i=0;i<restaurants.getLength();i++){
Element restaurant=(Element) restaurants.item(i);
if(Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==delId){
recordExist=true;
intI=i;
}
}
if(!recordExist){
out.print(showDialog(" 你要刪除餐館的記錄不存在 !"));
return;
}else{
// 進行記錄刪除的操作
try{
Node delNode=(Node)restaurants.item(intI);
doc.getElementsByTagName("restaurants").item(0).removeChild(delNode);
// 調用 bean 寫入相應的 xml 文件
writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
response.sendRedirect(request.getRequestURI());
return;
}catch(Exception e){}
}
}
}
// 由外部傳入參數決定頁面相應的處理狀態
if (strAct==null){
strOperation="show";
}else{
if (strAct.equals("modi")){
strOperation="modi";
intId=Integer.parseInt(request.getParameter("recordId"));
}else{
if(strAct.equals("addnew")){
strOperation="addnew";
}else{
strOperation="show";
}
}
}
// 如果為空記錄,則變更頁面狀態為“新增”
if (restaurants.getLength()==0){
strOperation="addnew";
}
%>
<html>
<head>
<title>oddWorld 餐飲系統 </title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="expires" content="0">
<link rel="stylesheet" href="../../include/itsp.css" type="text/css">
</head>
<body >
<div align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0" height="22">
<tr>
<td width="1"><img src="../../http://www.knowsky.com/images/top_r1.GIF" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統管理 -- 餐館管理 </td>
<td><img src="../../http://www.knowsky.com/images/top_r2.GIF" width="294" height="22"></td>
<td width=100 align="center"><a href="/index.html">[ 退出系統 ]</a></td>
</tr>
</table>
<br>
<br>
<table bgcolor="#999999" align=center border=0 cellpadding=1 cellspacing=1
width="90%">
<tbody>
<tr bgcolor="#efefef" align="center" valign="middle">
<td class=ttTable height=30 width="20"> </td>
<td class=ttTable height=30 width="0"> 餐館名稱 </td>
<td class=ttTable height=30 width="0"> 餐館電話 </td>
<td class=ttTable height=30 width="0">
<div align="center"> 餐館地址 </div>
</td>
<td class=ttTable height=30 width="30">
<div align="center"> 修改 </div>
</td>
<td class=ttTable height=30 width="30">
<div align="center"> 刪除 </div>
</td>
</tr>
<%
for(int i=0;i<restaurants.getLength();i++)
{
Element restaurant=(Element) restaurants.item(i);
if (strOperation=="modi" && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())==intId){
%>
<%// 顯示修改的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform action="<%=request.getRequestURI()%>?act=modiDo" method="post" onSubmit=''return checkform(this);'' >
<td class=tdsmall height=25 width="20">
<input type="hidden" name="recordId" value="<%=restaurant.getAttributeNode("id").getNodeValue()%>">
<%=(i+1)%></td>
<td class=tdsmall height=25>
<input name="name" class=stedit
style="HEIGHT: 22px; WIDTH: 150px" value="<%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
}%>
" maxlength="40" >
</td>
<td class=tdsmall height=25>
<input name="phone" class=stedit
style="HEIGHT: 22px; WIDTH: 100px" value="<%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
}%>" maxlength="20" >
</td>
<td class=tdsmall height=25>
<input name="address" class=stedit
style="HEIGHT: 22px; WIDTH: 200px" value="<%
if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
}%>" maxlength="100" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform()==false);"><img border=0
height=15 src="http://www.knowsky.com/images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% }else{
// 顯示正常的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<td class=tdsmall height=25 width="20"><%=(i+1)%></td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("name").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="0"><%if(restaurant.getElementsByTagName("phone").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("phone").item(0).getFirstChild().getNodeValue());
}%></td>
<td class=tdsmall height=25 width="0">
<%
if(restaurant.getElementsByTagName("address").item(0).hasChildNodes()){
out.print(restaurant.getElementsByTagName("address").item(0).getFirstChild().getNodeValue());
}%>
</td>
<td class=tdsmall height=25 width="30"><a href="<%=request.getRequestURI()%>?act=modi&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()%>"><img border=0
height=15 src="http://www.knowsky.com/images/edit.gif" width=15></a></td>
<td class=tdsmall height=25 width="30"><img border=0
height=15
onClick="javascript:if(confirm('' 您是否確定刪除本記錄,刪除后將導至記錄無法使用? '')){window.location.href=''<%=request.getRequestURI()%>?act=del&recordId=<%=restaurant.getAttributeNode("id").getNodeValue()%>'';}"
src="http://www.knowsky.com/images/delete.gif" style="CURSOR: hand" width=15> </td>
</tr>
<% }
}%>
<% if (strOperation=="addnew"){
// 顯示新增的格式 %>
<tr align="center" bgcolor="#ffffff" valign="middle">
<form name=dataform2 action="<%=request.getRequestURI()%>?act=addnewDo" method="post" onSubmit=''return checkform2(this);'' >
<td class=tdsmall height=25 width="20"></td>
<td class=tdsmall height=25>
<input name="name" class=stedit
style="HEIGHT: 22px; WIDTH: 150px" value="" maxlength="40" >
</td>
<td class=tdsmall height=25>
<input name="phone" class=stedit
style="HEIGHT: 22px; WIDTH: 100px" value="" maxlength="20" >
</td>
<td class=tdsmall height=25>
<input name="address" class=stedit
style="HEIGHT: 22px; WIDTH: 200px" value="" maxlength="100" >
</td>
<td class=tdsmall height=25 width="25"><a href="javascript:if (checkform2()==false);"><img border=0
height=15 src="http://www.knowsky.com/images/editok.gif" width=15></a></td>
<td class=tdsmall height=25 width="25"> </td>
</form>
</tr>
<% } %>
</tbody>
</table>
<br>
<table align=center border=0 cellpadding=0 cellspacing=2 width="95%">
<tbody>
<tr valign=center>
<td align=middle> <br>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td>
<% if (strOperation=="addnew"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform2()==false);" type=button value=" 更新記錄 ">
<% }else{
if(strOperation=="modi"){
%>
<input class=stbtm name=update onClick="javascript:if (checkform()==false);" type=button value=" 更新記錄 ">
<%
}else{
%>
<input class=stbtm type="button" name="Button" value=" 新 增 " onClick="javascript:window.location.href=''<%=request.getRequestURI()%>?act=addnew'';"><%
}
} %>
</td>
<td>
<input class=stbtm type="button" name="Button" value=" 返 回 " onClick="javascript:window.location.href=''index.jsp'';">
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> </p>
</div>
</body>
</html>
<script LANGUAGE=javascript>
<!--
function checkform2()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform2.name.value))
{
alert(" 餐館名稱不能為空! ");
dataform2.name.focus();
return false;
}
if (Checkblank.test(dataform2.phone.value))
{
alert(" 餐館電話不能為空! ");
dataform2.phone.focus();
return false;
}
window.dataform2.submit();
}
function checkform()
{
var Checkblank = /^(\s*|(\ )|(\.))*$/;
if (Checkblank.test(dataform.name.value))
{
alert(" 餐館名稱不能為空! ");
dataform.name.focus();
return false;
}
if (Checkblank.test(dataform.phone.value))
{
alert(" 餐館電話不能為空! ");
dataform.phone.focus();
return false;
}
window.dataform.submit();
}
-->
</script>
說明:本文件的書寫有很多地方并不簡練,因為在程式的開發過程中,過分簡練的程序往往會帶來后期維護的困難。
開發心得:
doc.getElementsByTagName("restaurants").item(int i) 的返回值是 node 型,如果不是要調用它的屬性值,沒有必要強制轉型為 Element 型。可以直接操作。本系統因為開發的參考資料的錯誤,所以全都采用了強制轉型。可以在以后的開發中考慮使用 node 直接進行操作。
trim() 和 Interger.parseInt() 函數都不可以接受 null 型的數值
在 tomcat 下左右的文件都是目錄從 TOMCAT 算起,具體情況請參見 \webapps\canyin\jsp\userjudge.jsp 里關于 xml 路徑的寫法。