Topquan's Blog

          分享價(jià)值----成就你我----我的博客----你的家

          一個(gè)JSP+JAVABEAN+XML開發(fā)例子

          本例子是參考了一些網(wǎng)站上有關(guān) JSP  對(duì)  XML  的操作的相關(guān)文檔,又結(jié)合了一些個(gè)人的體會(huì)。例子涉及的內(nèi)容是,開發(fā)的一個(gè)企業(yè)內(nèi)部定餐系統(tǒng)后臺(tái)管理端的部分代碼,功能主要集中在對(duì)于餐館基本信息的管理。

          該例子本身開發(fā)的起因是我在原公司和同事們一個(gè)玩笑的一部分。特此也表達(dá)對(duì)那些一起共事的朋友們的想念。

          例子本身是在 TOMCAT4.01  平臺(tái)下運(yùn)行的 B/S 結(jié)構(gòu)的程式。有關(guān) TOMCAT  的配置,這里不做說明。只講解一下相關(guān)文件及文件夾的目錄結(jié)構(gòu)。

          目錄結(jié)構(gòu)說明:
          /tomcat/webapps/canyin/                    -----
          主目錄
          /tomcat/webapps/canyin/jsp/               -----JSP 
          文件目錄
          /tomcat/webapps/canyin/jsp/admin/       -----
          實(shí)現(xiàn)后臺(tái)管理的 JSP  文件的存放目錄
          /tomcat/webapps/canyin/WEB-INF/classes/canyin/               ------javabean 
          文件的存放目錄
          /tomcat/webapps/canyin/data/   -----xml 
          文件存放目錄
          /tomcat/webapps/ROOT/           -----tomcat 
          啟動(dòng)文件存放文件夾,只存放了 index.html  文件

          文件簡單說明:
          /tomcat/webapps/canyin/data/users.xml    -----
          記錄用戶信息
          /tomcat/webapps/canyin/data/restaurants.xml  -----
          記錄餐館的基礎(chǔ)信息


          /tomcat/webapps/ROOT/index.html       -----
          首頁,頁面出現(xiàn)輸入框,要求用戶輸入用戶名,密碼


          /tomcat/webapps/canyin/jsp/loginjudge.jsp       -----
          用戶身份判斷頁面,根據(jù)用戶名稱和密碼決定頁面是轉(zhuǎn)入后臺(tái)管理端,還是前臺(tái)客戶端。本例子中,用戶身份一旦確認(rèn)為有管理權(quán)限,可以進(jìn)入后臺(tái)管理端,就直接跳到餐館基本信息管理頁面,簡化說明的流程。
          /tomcat/webapps/canyin/jsp/admin/admin_rest.jsp    -----
          餐館基本信息管理頁面,管理餐館的名稱,電話,地址等信息

          /tomcat/webapps/canyin/WEB-INF/classes/canyin/checkSessionBean.class  ----- 
          后臺(tái)管理端檢測標(biāo)志用戶身份的 session  的值,如果不是管理員的話,跳回登陸頁面。               
          /tomcat/webapps/canyin/WEB-INF/classes/canyin/connXmlBean.class  -----
          連接 xml  文件
          /tomcat/webapps/canyin/WEB-INF/classes/canyin/writeXmlBean.class  -----
          寫入 xml 文件

          文件詳細(xì)介紹及附帶代碼說明。

          /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 號(hào) </address> 
            </restaurant>
            </restaurants>
          說明: <num> 屬性是記錄在 restaurants.xml  文件中總共有過多少條記錄,每新增一條,無論以后刪除是否,該值都會(huì)增加 1 ,就好象數(shù)據(jù)庫中習(xí)慣使用的自動(dòng)增加 1 id  項(xiàng)。用來給新增的  <restaurant> 的屬性 <id> 賦一個(gè)唯一的值。其它的字段意思比較明顯。
          /tomcat/webapps/ROOT/index.html       
          (單純的 HTML 代碼)

          代碼:
          <html>
          <head>
          <title>oddWorld 
          餐飲系統(tǒng) </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"> 
          餐飲系統(tǒng)登錄  </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">
          餐飲系統(tǒng)登錄 </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  
          (代碼是相應(yīng)的 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);
            }
           }
          }

          說明:檢驗(yàn)參數(shù)傳入的 session  名稱的數(shù)值和參數(shù)傳入的字段的數(shù)值是否相等。

          /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);
           }
          }

          說明:打開一個(gè)指定 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  的內(nèi)容到一個(gè)指定的 xml  文件。

          /tomcat/webapps/canyin/jsp/loginjudge.jsp       

          代碼:
          <%--  oddWorld 
          餐飲管理系統(tǒng) ( 簡體中文版 ) 2002 12 1
           copy right by joard ast  
           
           loginjudge.jsp 
          功能:用戶身份校驗(yàn),根據(jù)  /data/user.xml  文件內(nèi)標(biāo)示的用戶不同的身份
           
          決定轉(zhuǎn)入后臺(tái)管理頁面,還是客戶點(diǎn)菜頁面。
           --%>

          <%@ 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");

          //
          校驗(yàn)數(shù)據(jù)是否為空
          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(''
          歡迎管理員登陸系統(tǒng) !'');");
              out.println("</script>");
              
              //
          設(shè)置標(biāo)示用戶身份的  session sesUserRole   ,管理員身份為  admin
              session.setAttribute("sesUserRole","admin");
              
              //
          跳轉(zhuǎn)到管理頁面
              response.sendRedirect("admin/admin_rest.jsp");
              return;
              
                   }else{
                    //
          設(shè)置標(biāo)示用戶身份的  session sesUserRole   ,管理員身份為  user
                    session.setAttribute("sesUserRole","user");
                    
                    //
          跳轉(zhuǎn)到普通用戶頁面
                    response.sendRedirect("index.jsp");       
                    return;
                   }

                  }else{
                   out.println("<script language=''javascript''>");
             out.println("alert(''
          用戶名或密碼錯(cuò)誤 !'');");
             out.println("history.go(-1);");
             out.println("</script>");
             return;
                  }

           }
          }catch(Exception e){
               strExc=e.toString();
          }
          %>
          說明: .......

          /tomcat/webapps/canyin/jsp/admin/admin_rest.jsp    

          代碼:
          <%--  oddWorld 
          餐飲管理系統(tǒng) ( 簡體中文版 ) 2002 12 1
           copy right by joard ast  
           
           admin_rest.jsp 
          功能:后臺(tái)管理頁面,餐館管理頁面。
           --%>

          <%@ 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" />

          <%//
          校驗(yàn)可戶身份,判斷是不是管理員
          if(!checkSessionBean.checkSessionBean(request,"sesUserRole","admin")){
           out.print(showDialog("
          您沒有管理的權(quán)限 !","/index.html"));
           return;
          }

          //
          從餐館資料文件  rest.xml  中得到相關(guān)數(shù)據(jù)
          Document doc;
          NodeList restaurants;

          String strAct;
          int intId=0;
          String strOperation="show";

          //
          接受外部傳入的參數(shù)
          strAct=(String)request.getParameter("act");

          xmlBean.connXml("webapps/canyin/data/restaurants.xml");
          doc=xmlBean.getXmlDoc();
          restaurants =doc.getElementsByTagName("restaurant");

          //
          根據(jù)外部傳入的參數(shù)來決定對(duì)  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();
            
            //
          數(shù)據(jù)校驗(yàn)
            if(strName==null){
             out.print(showDialog("
          餐館名稱不能為空 !"));
             return;
            }
            if(strPhone==null){
             out.print(showDialog("
          餐館電話不能為空 !"));
             return;
            }
            /*if(strAddress==null){
             out.print(showDialog("
          餐館地址不能為空 !"));
             return;
            }*/
            
            //
          校驗(yàn)數(shù)據(jù)的唯一性
            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("
          餐館名稱重復(fù) !"));
              return; 
             }else{
              if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone)){
               out.print(showDialog("
          餐館電話重復(fù) !"));
               return;
              }
             }
             
            }
            
            
              
            //
          得到已有的記錄數(shù),給新增的餐館記錄設(shè)定唯一的遞增的 id  屬性
            int intNum=0;
            Element restNum=(Element)doc.getElementsByTagName("restaurants").item(0);
            intNum=Integer.parseInt(restNum.getAttributeNode("num").getNodeValue()); 

            intNum+=1;
            
            //
          restaurants 的屬性 num  的數(shù)值加 1
            restNum.getAttributeNode("num").setNodeValue(String.valueOf(intNum));

            //
          新增節(jié)點(diǎn)     
            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);

            //
          調(diào)用 bean  寫入相應(yīng)的 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) 的哪一項(xiàng)
            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());
            
            //
          數(shù)據(jù)校驗(yàn)
            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;
            }*/
            
            //
          標(biāo)志顯示記錄存在
            boolean recordExist=false;
            
            //
          校驗(yàn)數(shù)據(jù)的唯一性
            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("
          餐館名稱重復(fù) !"));
              return; 
             }else{
              if(((String)restaurant.getElementsByTagName("name").item(0).getFirstChild().getNodeValue()).equals(strPhone) && Integer.parseInt(restaurant.getAttributeNode("id").getNodeValue())!=modiId ){
               out.print(showDialog("
          餐館電話重復(fù) !"));
               return;
              }
             }
             
            }
            

            
            if(!recordExist){
             out.print(showDialog("
          你要修改餐館的記錄不存在 !"));
             return;
            }else{
             //
          進(jìn)行記錄更改的操作
             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);
              
              //
          調(diào)用 bean  寫入相應(yīng)的 xml 文件
              writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");
            
              response.sendRedirect(request.getRequestURI());  
              return; 
              
             }catch(Exception e){}
            }
           }
           //
          進(jìn)行刪除操作
           if(strAct.equals("del")){
            int delId;
            //
          記錄要修改的記錄是 item(i) 的哪一項(xiàng)
            int intI=0;

            delId=Integer.parseInt(request.getParameter("recordId").trim());

            if(delId==0){
             out.print(showDialog("
          你要修改餐館的記錄不存在 !"));
             return;
            }
            
            file://
          標(biāo)志顯示記錄存在
            boolean recordExist=false;

            //
          校驗(yàn)數(shù)據(jù)的唯一性
            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("
          你要?jiǎng)h除餐館的記錄不存在 !"));
             return;
            }else{
             //
          進(jìn)行記錄刪除的操作
             try{
              Node delNode=(Node)restaurants.item(intI);
              
              doc.getElementsByTagName("restaurants").item(0).removeChild(delNode);

              //
          調(diào)用 bean  寫入相應(yīng)的 xml 文件
              writeXmlBean.writeXml(doc,"webapps/canyin/data/restaurants.xml");

              response.sendRedirect(request.getRequestURI());  
              return; 
              
             }catch(Exception e){}
            }

           }
          }

          //
          由外部傳入?yún)?shù)決定頁面相應(yīng)的處理狀態(tài)
          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";
            }
           }
          }


          //
          如果為空記錄,則變更頁面狀態(tài)為“新增”
          if (restaurants.getLength()==0){
           strOperation="addnew";
          }
          %>

          <html>
          <head>
          <title>oddWorld 
          餐飲系統(tǒng) </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"> 
          餐飲系統(tǒng)管理 -- 餐館管理 </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">[ 
          退出系統(tǒng)  ]</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(''
          您是否確定刪除本記錄,刪除后將導(dǎo)至記錄無法使用? '')){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>
          說明:本文件的書寫有很多地方并不簡練,因?yàn)樵诔淌降拈_發(fā)過程中,過分簡練的程序往往會(huì)帶來后期維護(hù)的困難。

          開發(fā)心得:

          doc.getElementsByTagName("restaurants").item(int i)
          的返回值是 node  型,如果不是要調(diào)用它的屬性值,沒有必要強(qiáng)制轉(zhuǎn)型為  Element 型。可以直接操作。本系統(tǒng)因?yàn)殚_發(fā)的參考資料的錯(cuò)誤,所以全都采用了強(qiáng)制轉(zhuǎn)型。可以在以后的開發(fā)中考慮使用 node  直接進(jìn)行操作。

          trim() 
           Interger.parseInt()  函數(shù)都不可以接受 null  型的數(shù)值

          tomcat  下左右的文件都是目錄從 TOMCAT  算起,具體情況請(qǐng)參見 \webapps\canyin\jsp\userjudge.jsp  里關(guān)于 xml  路徑的寫法。

          posted on 2006-05-08 16:36 topquan 閱讀(254) 評(píng)論(0)  編輯  收藏 所屬分類: JSP&Servlet


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 分宜县| 建宁县| 远安县| 石渠县| 东至县| 新田县| 洛浦县| 辉县市| 西畴县| 大足县| 香格里拉县| 礼泉县| 临潭县| 沙河市| 黔西| 清苑县| 安龙县| 湛江市| 长宁区| 历史| 延安市| 青阳县| 三原县| 日照市| 行唐县| 民权县| 葫芦岛市| 墨竹工卡县| 通化市| 开阳县| 海伦市| 民权县| 德江县| 巩留县| 昔阳县| 高安市| 东安县| 老河口市| 常宁市| 波密县| 榆林市|