|
【java大本营】http://www.javadby.com ,java实力站点,更新速度快,内容全面,鼓励原创,吸引了相当数量的编程学习者。欢迎加入java交流群41970496,共同进步。
以下是postingXML.html的内容:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/1999/xhtml"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Sending an XML Request</title> <script type="text/javascript"> var xmlHttp; /** *创建XMLHttpRequest对象,此对象是AJAX核心,用于发送异步请求 * */ function createXMLHttpRequest(){ if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); }else if (window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } /** *生成xml字符串 * */ 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+="<type>" +option.value+"<\/type>"; } } xml+="<\/pets>"; return xml; } /** *发送POST请求 * */ function sendPetTypes(){ createXMLHttpRequest(); var xml=createXML();
//我的项目叫Ajax1所以应该更改成你的项目名
//timeStamp=new Date().getTime()是用来生成一个时间戳用于防止url被缓存:因为每次new Date().getTime 方法都会生一个long的值,此值不会重复所以每次的url都是唯一的,所以就不可能去缓存中读数据了。 var url="/Ajax1/PostingXMLExample?timeStamp="+new Date().getTime(); xmlHttp.open("POST",url,true); xmlHttp.onreadystatechange = handleStateChange; //Post请求所必须设置的请求头信息 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttp.send(xml); } function handleStateChange(){ if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ //成功返回后调用此方法 parseResults(); } } } function parseResults(){ var responseDiv = document.getElementById("serverResponse"); if(responseDiv.hasChildNodes()){ //删除div中的所有信息为显示最新信息做准备 responseDiv.removeChild(responseDiv.childNodes[0]); } //用返回的字符串生成一个文本节点 var responseText = document.createTextNode(xmlHttp.responseText); //把文本节点作为div的子节点显示出来 responseDiv.appendChild(responseText); } </script> </head>
<body> <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="rebbits">Rebbits</option> </select> <br/><br/> <input type="button" value="Submit Pets" onclick="sendPetTypes();"/> </form> <h2>Server Response:</h2> <div id="serverResponse"></div> </body> </html> 以下是servlet:PostingXMLExample.java的代码清单:
package servlet;
import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilderFactory; 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 {
/** * Constructor of the object. */ public PostingXMLExample() { super(); }
/** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
} 共2页: 上一页 1 [2] 下一页
|