1. 程式人生 > >ajax_servlet數據交互實例(一)

ajax_servlet數據交互實例(一)

post mic encode file 異步 pla header 獲取數據 win

java代碼

package action;

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;

/**
* Servlet implementation class getAjaxServlet
*/
public class getAjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String age=request.getParameter("age");
System.out.println("name:"+name+","+"age:"+age);
PrintWriter out=response.getWriter();
out.println("成功從Servlet拿到數據");
out.flush();
out.close();

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}

}

jsp頁面:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<div style="text-align: center;">
<br/>
<br/>
<div>
<a href="javascript:alert(getAjax());">獲取ajax對象</a><br/>
<input type="button" value="ajax獲取數據get" onclick="loadNameGet();"/>
<input type="button" value="ajax獲取數據post" onclick="loadNamePost();"/>
<input type="text" value="" id="name" name="name"></input>
<a id="ajax"></a>
</div>
</div>
</body>
<script type="text/javascript">
function getAjax() {//封裝獲取創建ajax對象的方法
var xhr = null;
if (window.XMLHttpRequest) {
// 非ie瀏覽器
xhr = new XMLHttpRequest();
} else {
// ie瀏覽器
xhr = new ActiveXObject(‘MicroSoft.XMLHttp‘);
}
return xhr;
}
function loadNameGet(){

var xhr=getAjax();
alert("readyState"+xhr.readyState+";"+"status"+xhr.status);
xhr.onreadystatechange = function(){
alert("readyState"+xhr.readyState+";"+"status"+xhr.status);
//處理服務器返回數據
if(xhr.readyState == 4 &&
xhr.status == 200){
var txt = xhr.responseText;
alert(txt);
document.getElementById("name").value=txt;
}
};
xhr.open("get",
"ajax.do?name=jack&age=20",//請求地址(web中配置地址),get可以拼接參數
true);//true異步
xhr.send(null);
}
function loadNamePost(){
var xhr=getAjax();
xhr.open("post","ajax.do",true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function(){
//處理服務器返回數據
if(xhr.readyState == 4 &&
xhr.status == 200){
var txt = xhr.responseText;
alert(txt);
document.getElementById("ajax").innerHTML=txt;
}
};
xhr.send("name=jack&age=10");//post請求參數
}
</script>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ajax02</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>ajax.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>action.getAjaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/ajax.do</url-pattern>
</servlet-mapping>
</web-app>

ajax_servlet數據交互實例(一)