Ajax实现xml文件数据插入数据库(二)--- ajax实现与jsp的数据交互

来源:互联网 发布:java软件工程培训 编辑:程序博客网 时间:2024/05/16 07:51

前文中我们介绍了用JavaScript解析xml文件的方法,本文承接上文中解析好的xml文件中的数据,现在需要将这些数据传递到后台。在这里我们首先用js将解析好的数据拼接成字符串,然后到jsp中再根据解析规则对传过来的数据进行拆解后重新组装,得到我们想要的格式。下面的代码首先通过Ajax将解析好的xml数据传递到jsp:

[javascript] view plain copy
  1. <span style="font-size:12px;">  <script     language="javascript">     
  2.         var xmlHttp=null;   
  3.         function readyStateChangeHandle() {   
  4.             if(xmlHttp.readyState==4) {   
  5.                 if(xmlHttp.status==200) {                     
  6.                     alert("请求成功");  
  7.                 }else{   
  8.                     alert("请求失败");   
  9.                 }   
  10.             }   
  11.         }   
  12.                           
  13.         function ajaxRequest(transData) {   
  14.             if(window.XMLHttpRequest) {   
  15.                 xmlHttp=new XMLHttpRequest();   
  16.             }else if(window.ActiveXObject){   
  17.                 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
  18.             }   
  19.                   
  20.             xmlHttp.onreadystatechange=readyStateChangeHandle;   
  21.             xmlHttp.open("POST","index.jsp?transData=" + transData,true);   
  22.             xmlHttp.send(null);   
  23.         }   
  24.   
  25.         function insertAllData(){  
  26.             var stringData="";  
  27.             var dataCollection = readAllChildNode('authority.xml','userId');  
  28.             for(var i=0;i<dataCollection.length;i++){  
  29.                 stringData = stringData+dataCollection[i] + ",";  
  30.             }  
  31.               
  32.             ajaxRequest(stringData);              
  33.         }</span>  

上述函数之间的调用过程是客户端通过点击按钮触发js中的insertAllData()函数,该函数在执行过程中首先调用原先构建的js库中的方法对xml文件进行解析,并将解析好的数据拼接成字符串,之后调用ajaxRequest()方法将拼接好的字符串以post方式传递给index.jsp。jsp取得传过来的字符串后将其重新按规则构建成我们想要的额数据的过程是这样的(jsp中代码):

[plain] view plain copy
  1. <span style="font-family:SimSun;font-size:12px;"><%@ page language="java" import="java.util.*" contentType="text/html; charset=GB18030"  
  2.  pageEncoding="GB18030"%>  
  3.  <%@ page import="UserManager.*" %>  
  4. <%  
  5.     //设置字符集   
  6.     request.setCharacterEncoding("GB18030");  
  7.     String path = request.getContextPath();  
  8.     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  9.     //取得客户端传来的拼接好的字符串  
  10.     String receiveStr = request.getParameter("transData");  
  11.     //去掉字符串最后的逗号  
  12.     receiveStr = receiveStr.substring(0, receiveStr.length() - 1);  
  13.     //将字符串以逗号为界拆分成数组  
  14.     String[] m =receiveStr.split(",");  
  15.     //实例化一个javaBean对象  
  16.     UserManager userManager = new UserManager();  
  17.     int x=0;      
  18.     for(int a = 0; a < m.length / 3;a++){                  
  19.         ArrayList<String> n1 = new ArrayList<String>();  
  20.         //该循环的作用是将字符串数组中的数据每三个装入一个ArrayList中  
  21.         for(int i =3*x ;i<3*x+3;i++){  
  22.             n1.add(m[i]);                         
  23.         }  
  24.         x++;  
  25.         //将填装了数据的ArrayList重新转换为字符串数组  
  26.         String[] values = (String[])n1.toArray(new String[0]);  
  27.         //调用javabean相关方法实现数据插入数据库  
  28.         userManager.insertUser(values[0], values[1], values[2]);  
  29.     }  
  30. %>  
  31.   
  32. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  33. <html>  
  34.   <head>  
  35.     <base href="<%=basePath%>">  
  36.       
  37.     <title>My JSP 'index.jsp' starting page</title>  
  38.     <meta http-equiv="pragma" content="no-cache">  
  39.     <meta http-equiv="cache-control" content="no-cache">  
  40.     <meta http-equiv="expires" content="0">      
  41.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  42.     <meta http-equiv="description" content="This is my page">  
  43.     <!--  
  44.     <link rel="stylesheet" type="text/css" href="styles.css">  
  45.     -->  
  46.   </head>  
  47.     
  48.   <body>  
  49.     This is my JSP page. <br>  
  50.   </body>  
  51. </html></span>  

上面的代码中我们将从客户端接收到的数据进行了拆分并根据一定的规则重新组织成新的结构,并调用了javaBean中的相关方法实现了数据的插入。在接下来的文章中我们将对本过程的最后一个环节(即调用javaBean相关方法进行插入)进行介绍。


0 0
原创粉丝点击