下面是index.jsp
index.jsp
1<%@ page language="java" pageEncoding="UTF-8"%>
2<html>
3 <head>
4 <title>My JSP 'index.jsp' starting page</title>
5 <script type="text/javascript" src="ReadXML.js"></script></head>
6 <body>
7 name <input type="text" id="name"><br>
8 age <input type="text" id="age"><br>
9 sex <input type="text" id="sex"><br>
10 <input type="button" value="SendByGet" onclick="sendByGet()">
11 <input type="button" value="SendByPost" onclick="sendByPost()"><br><br>
12 <div id="showDiv"></div>
13 </body>
14</html>
1<%@ page language="java" pageEncoding="UTF-8"%>
2<html>
3 <head>
4 <title>My JSP 'index.jsp' starting page</title>
5 <script type="text/javascript" src="ReadXML.js"></script></head>
6 <body>
7 name <input type="text" id="name"><br>
8 age <input type="text" id="age"><br>
9 sex <input type="text" id="sex"><br>
10 <input type="button" value="SendByGet" onclick="sendByGet()">
11 <input type="button" value="SendByPost" onclick="sendByPost()"><br><br>
12 <div id="showDiv"></div>
13 </body>
14</html>
ReadXML.js
1var xmlHttp;
2
3function createXmlHttpRequest(){
4 if(window.XmlHttpRequest){
5 xmlHttp = new XmlHttpRequest();
6 }else if(window.ActiveXObject){
7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
8 }
9}
10
11function createQueryString(){
12 var name = document.getElementById("name").value;
13 var age = document.getElementById("age").value;
14 var sex = document.getElementById("sex").value;
15 var queryString = "name="+name+"&age="+age+"&sex="+sex;
16 return queryString;
17}
18
19function sendByGet(){
20 createXmlHttpRequest();
21 var queryString = "message?"+createQueryString()+"&timeStemp="+new Date().getTime();
22 xmlHttp.onreadystatechange=handleStateChange;
23 xmlHttp.open("GET",queryString,true);
24 xmlHttp.send();
25}
26
27function sendByPost(){
28 createXmlHttpRequest();
29 //在url上加一个当前时间是为了使每次发送的url唯一,防止浏览器缓存。
30 var url = "message?timeStemp="+new Date().getTime();
31 var queryString=createQueryString();
32 xmlHttp.onreadystatechange=handleStateChange;
33 xmlHttp.open("POST",url,true);
34 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
35 xmlHttp.send(queryString);
36}
37
38function handleStateChange(){
39 var showDiv = document.getElementById("show");
40 if(xmlHttp.readyState==4){
41 if(xmlHttp.status==200){
42 var showDiv = document.getElementById("showDiv");
43 if(showDiv.hasChildNodes()){
44 showDiv.removeChild(showDiv.childNodes[0]);
45 }
46 //创建创建textNode
47 var responseText = document.createTextNode(xmlHttp.responseText);
48 //添加到showDiv元素中
49 showDiv.appendChild(responseText);
50 }
51 }
52}
1var xmlHttp;
2
3function createXmlHttpRequest(){
4 if(window.XmlHttpRequest){
5 xmlHttp = new XmlHttpRequest();
6 }else if(window.ActiveXObject){
7 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
8 }
9}
10
11function createQueryString(){
12 var name = document.getElementById("name").value;
13 var age = document.getElementById("age").value;
14 var sex = document.getElementById("sex").value;
15 var queryString = "name="+name+"&age="+age+"&sex="+sex;
16 return queryString;
17}
18
19function sendByGet(){
20 createXmlHttpRequest();
21 var queryString = "message?"+createQueryString()+"&timeStemp="+new Date().getTime();
22 xmlHttp.onreadystatechange=handleStateChange;
23 xmlHttp.open("GET",queryString,true);
24 xmlHttp.send();
25}
26
27function sendByPost(){
28 createXmlHttpRequest();
29 //在url上加一个当前时间是为了使每次发送的url唯一,防止浏览器缓存。
30 var url = "message?timeStemp="+new Date().getTime();
31 var queryString=createQueryString();
32 xmlHttp.onreadystatechange=handleStateChange;
33 xmlHttp.open("POST",url,true);
34 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
35 xmlHttp.send(queryString);
36}
37
38function handleStateChange(){
39 var showDiv = document.getElementById("show");
40 if(xmlHttp.readyState==4){
41 if(xmlHttp.status==200){
42 var showDiv = document.getElementById("showDiv");
43 if(showDiv.hasChildNodes()){
44 showDiv.removeChild(showDiv.childNodes[0]);
45 }
46 //创建创建textNode
47 var responseText = document.createTextNode(xmlHttp.responseText);
48 //添加到showDiv元素中
49 showDiv.appendChild(responseText);
50 }
51 }
52}
后台servlet
Message.java
1package com.kai;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5
6import javax.servlet.ServletException;
7import javax.servlet.http.HttpServlet;
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11public class Message extends HttpServlet {
12
13 private static final long serialVersionUID = 7196636596362343837L;
14
15 public void doGet(HttpServletRequest request, HttpServletResponse response)
16 throws ServletException, IOException {
17 response.setContentType("text/html");
18 PrintWriter out = response.getWriter();
19 String name = request.getParameter("name").toString();
20 String age = request.getParameter("age").toString();
21 String sex = request.getParameter("sex").toString();
22 String resText = "you name is "+name+"; age: "+age+"; sex: "+sex;
23 out.println(resText);
24 out.flush();
25 out.close();
26 }
27
28 public void doPost(HttpServletRequest request, HttpServletResponse response)
29 throws ServletException, IOException {
30 doGet(request,response);
31 }
32
33}
1package com.kai;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5
6import javax.servlet.ServletException;
7import javax.servlet.http.HttpServlet;
8import javax.servlet.http.HttpServletRequest;
9import javax.servlet.http.HttpServletResponse;
10
11public class Message extends HttpServlet {
12
13 private static final long serialVersionUID = 7196636596362343837L;
14
15 public void doGet(HttpServletRequest request, HttpServletResponse response)
16 throws ServletException, IOException {
17 response.setContentType("text/html");
18 PrintWriter out = response.getWriter();
19 String name = request.getParameter("name").toString();
20 String age = request.getParameter("age").toString();
21 String sex = request.getParameter("sex").toString();
22 String resText = "you name is "+name+"; age: "+age+"; sex: "+sex;
23 out.println(resText);
24 out.flush();
25 out.close();
26 }
27
28 public void doPost(HttpServletRequest request, HttpServletResponse response)
29 throws ServletException, IOException {
30 doGet(request,response);
31 }
32
33}