电脑软硬件应用网
当前位置: 电脑软硬件应用网 > 设计学院 > 网络编程 > javascript > 正文
使用JSP + JAVABEAN + XML 开发的一个例子
使用JSP + JAVABEAN + XML 开发的一个例子
2005-12-30 19:21:23  文/45IT收集   出处:电脑软硬件应用网   
;(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.g

上一页  [1] [2] [3] [4] [5] [6] 下一页

  • 上一篇文章:

  • 下一篇文章:
  • 最新热点 最新推荐 相关文章
    javascript实用技巧点滴(三)
    javascript实用技巧点滴(2)
    javascript实用技巧点滴(1)
    javascript教程 - 第一课 1.1
    用js制作完善的日,月组合下拉框
    利用JSP编程建立动态Web站点
    用PHP制作留言板
    PHP生成动态WAP页面
    PHP实现文件下载
    PHP4的session功能
    关于45IT | About 45IT | 联系方式 | 版权声明 | 网站导航 |

    Copyright © 2003-2011 45IT. All Rights Reserved 浙ICP备09049068号