注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

断尘居

温柔的男人像海洋。

 
 
 
 
 

日志

 
 

AJAX 发送XML参数到servlet  

2010-09-17 03:54:05|  分类: JS / jQuery |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>

<BODY>
<script type="text/javascript">
 var xmlHttp;
 function createXMLHttpRequest()
 {
  if(window.ActiveXObject)
  {
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }else if(window.XMLHttpRequest)
  {
   xmlHttp=new XMLHttpRequest();
  }
 }

 function createXML()
 {
  var xml="<pets>";
  var options=document.getElementById("petTypes").childNodes;
  var option=null;
  for(var i=0;i<options.length;i++)
  {
   option=options[i];
   if(option.selected)
    {
    xml=xml+"<type>"+option.value+"<\/type>";
       }
  }
  xml=xml+"<\/pets>";
  return xml;
 }

 function sendPetTypes()
 {
  createXMLHttpRequest();
  var xml=createXML();
  var url="PostingXMLExample?timeStamp="+new Date().getTime();
  //alert(url);
  xmlHttp.open("POST",url,true);
  xmlHttp.onreadystatechange=handleStateChange;
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
  xmlHttp.send(xml);
 }

 function handleStateChange()
 { 
  if(xmlHttp.readyState==4)
  {  //alert(xmlHttp.status);
   if(xmlHttp.status==200)
   {
    parseResults();
    
   }
  }
 }

 function parseResults()
 {
  //alert("11");
  var responseDiv=document.getElementById("serverResponse");
  if(responseDiv.hasChildNodes())
  {
   responseDiv.removeChild(responseDiv.childNodes[0]);
  }
  var responseText=document.createTextNode(xmlHttp.responseText);
  responseDiv.appendChild(responseText);
 }
</script>
<h1>select the types of pets in your home:<h1>
<form action="#">
 <select id="petTypes" size="6" multiple="true">
  <option value="cats">cats</option>
  <option value="dogs">dogs</option>
  <option value="fish">fish</option>
  <option value="birds">birds</option>
  <option value="hamsters">hamsters</option>
  <option value="rabbits">rabbits</option>
 </select>
 <br><br>
 <input type="button" value="submit pets" onclick="sendPetTypes();"/>
 <h2>Server Response:<h2>
 <div id="serverResponse"></div>
</form>
</BODY>
</HTML>
PostingXMLExample.java---------------------------------------

package com.ajax;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;

//import javax.xml.parsers.*;
//import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class PostingXMLExample extends HttpServlet
{
 public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
 {
  String xml=readXMLFromRequestBody(request);
  Document xmlDoc=null;
  try
  {
   DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
   DocumentBuilder documentBuilder=builderFactory.newDocumentBuilder();
   xmlDoc=documentBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
  }catch(ParserConfigurationException e)
  {
   System.out.println("ParserConfigurationException:"+e.toString());
  }catch(SAXException e)
  {
   System.out.println("SAXException:"+e);
  }
  NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");
  String type=null;
  String responseText="seleted Pets:";
  for(int i=0;i<selectedPetTypes.getLength();i++)
  {
   type=selectedPetTypes.item(i).getFirstChild().getNodeValue();
   responseText=responseText+" "+type;
  }
  response.setContentType("text/xml");
  response.getWriter().println(responseText);
 
 }

 public String readXMLFromRequestBody(HttpServletRequest request)
 {
  StringBuffer xml=new StringBuffer();
  String line=null;
  try
  {
   BufferedReader reader=request.getReader();
   while((line=reader.readLine())!=null)
   {
    xml.append(line);
   }
  }catch(Exception e)
  {
   System.out.println("Error reading xml:"+e.toString());
  }
  return xml.toString();
 }
}

  评论这张
 
阅读(1206)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017