ajax_servlet数据交互实例(一)
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>