JSP 對 XML 的操作
2024-09-05 00:19:11
供稿:網友
本例子是參考了一些網站上有關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.163design.net/j/f/"images/top_r1.gif" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統登錄 </td>
<td><img src=http://www.163design.net/j/f/"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.163design.net/j/f/"..images/top_r1.gif" width="62" height="22"></td>
<td width=150 align="center"> 餐飲系統管理--餐館管理</td>
<td><img src=http://www.163design.net/j/f/"..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.163design.net/j/f/"..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.163design.net/j/f/"..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.163design.net/j/f/"..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.163design.net/j/f/"..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 路徑的寫法。