ajax获取servlet发送的数据
XMLHTTP中readState与status的状态值 当readState为4 status为200时说明成功 通过responseText获取servlet的文本 jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script> function getContent() { var xmlHttp; xmlHttp = createXmlHttp(); alert("readyState:"+xmlHttp.readyState + "status:"+xmlHttp.status); xmlHttp.onreadystatechange=function (){ alert("readyState:"+xmlHttp.readyState + "status:"+xmlHttp.status); if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ alert(xmlHttp.responseText); var text = document.getElementById("text"); text.value=xmlHttp.responseText; } }; xmlHttp.open("get","servlet",true); xmlHttp.send(null); } function createXmlHttp() { var xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } //兼容IE else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); if(!xmlHttp) { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } } return xmlHttp; } </script>
</head>
<body>
<input type="text" id="text">
<button id="btn" onclick="getContent()">获取ajax数据</button>
</body>
</html>
servletpackage com.cyh.test;
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;
public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("后台发送的数据");
}
public void doPost(HttpServletRequest request,IOException {
}
}
若servlet发送json格式package com.cyh.test;
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;
public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request,IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//json格式{k : v,k : v}
String content = "{"name":"张三","age":"18"}";
PrintWriter out = response.getWriter();
out.println(content);
}
public void doPost(HttpServletRequest request,IOException {
System.out.println("post");
}
}
前台接受<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script> function getContent() { var xmlHttp; xmlHttp = createXmlHttp(); xmlHttp.onreadystatechange=function (){ if (xmlHttp.readyState == 4 && xmlHttp.status == 200){ alert(xmlHttp.responseText); } }; xmlHttp.open("get",true); xmlHttp.send(null); } function createXmlHttp() { var xmlHttp; if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } //兼容IE else if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); if(!xmlHttp) { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } } return xmlHttp; } </script>
</head>
<body>
姓名<input type="text" id="name"><br>
年龄<input type="text" id="age"><br>
<button id="btn" onclick="getContent()">获取ajax数据</button>
</body>
</html>
返回的形式是{“name”:”张三”,“age”:”18”} 导入json jar包在servlet中可以这样设置 JSONObject content = new JSONObject(); content.put(“name”,“张三”); content.put(“age”,”19”); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |