用servlet做的
jsp文件:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test1.jsp' starting page</title>
<Meta http-equiv="pragma" content="no-cache">
<Meta http-equiv="cache-control" content="no-cache">
<Meta http-equiv="expires" content="0">
<Meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<Meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="${pageContext.request.contextPath }/ajaxget/test1.js">
</script>
</head>
<body>
<form action="" enctype="application/x-www-form-urlencoded" ><!-- 默认的application/x-www-form-urlencoded -->
<input type="button" name="ok" id="ok" value="测试服务器连接">
</form>
</body>
</html>
js文件:
/*
* AJAX建立和服务器的连接,接收服务器的请求,处理服务器返回的数据* 开发步骤
* *1.创建XMLHttpRequest对象
* *2.接收服务器的响应
* *3.打开和服务器的连接
* *4.发送数据
* *
*
*/
/*创建XMLHttpRequest对象*/
function ajaxFunction() {
var xmlHttp;
try{
//Firefox,opera 8.0+,safari
xmlHttp = new XMLHttpRequest();
}
catch(e){
try {
//IE 5.0
xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
}
catch(e){
try{
//IE 5.5 及更高版本
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
return xmlHttp;
}
window.onload = function() {
document.getElementById("ok").onclick = function() {
//alert("zhixing");
//1.获取xmlHttpRequest对象
var xmlReq = ajaxFunction();
alert(xmlReq);
//2.接收服务器的响应
/**
* XMLHttpRequest对象中有一个readyState属性
* 0 代表未初始化。 还没有调用 open 方法
1 代表正在加载。 open 方法已被调用,但 send 方法还没有被调用
2 代表已加载完毕。send 已被调用。请求已经开始
3 代表交互中。服务器正在发送响应
4 代表完成。响应发送完毕
每次 readyState 值的改变,都会触发 onreadystatechange 事件。
* **/
xmlReq.onreadystatechange = function() {
//alert(xmlReq.readyState);
//alert(xmlReq.status);
//响应发送完毕
if(xmlReq.readyState==4) {
//通过把这个值和200(服务器处理成功)或304(文件没有被修改)
if(xmlReq.status==200||xmlReq.status==304){
//alert("****");
//接收服务器端返回的的数据
var date = xmlReq.responseText;
alert("接收到的服务器端返回来的数据:"+date);
}
}
};
//3.打开和服务器的连接
/**
* open(method,url,asynch)
* * method 请求方法 :get post
* * url 请求的路径
* * asynch:表示请求是否要异步传输,默认是(true)异步
* **/
xmlReq.open("get","Testservlet?timeStemp="+new Date().getTime()+"&a=8",true);
//4.发送数据
/**
* 若选用的是get请求,则不会发送任何数据(如果用xmlReq.send("a=2&b=4")则后台收不到数据,此时可以用post方法),给send传递null即可
* **/
xmlReq.send("c=22&b=222");//注意这样后台是接收不到值的,我这样写是为了试验的哈哈哈,get时可以这样写xmlReq.send(null);
};
};
servlet文件
package cn.itcast.web; 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; //此类是练习ajaxget,和ajaxpost的 public class Testservlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { this.doPost(request,response); } public void doPost(HttpServletRequest request,IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); System.out.println("Testservlet执行......"); //加载的请求方法 System.out.println("加载的请求方法:"+request.getMethod()); //接收的请求参数 System.out.println("request.getParameter(a)"+request.getParameter("a")); System.out.println("request.getParameter(b)"+request.getParameter("b")); System.out.println("request.getParameter(c)"+request.getParameter("c")); //发送给客户端 out.print("测试成功"); } }package cn.itcast.web; 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; //此类是练习ajaxget,和ajaxpost的 public class Testservlet extends HttpServlet { public void doGet(HttpServletRequest request,response); } public void doPost(HttpServletRequest request,IOException { response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); System.out.println("Testservlet执行......"); //加载的请求方法 System.out.println("加载的请求方法:"+request.getMethod()); //接收的请求参数 System.out.println("request.getParameter(a)"+request.getParameter("a")); System.out.println("request.getParameter(b)"+request.getParameter("b")); System.out.println("request.getParameter(c)"+request.getParameter("c")); //发送给客户端 out.print("测试成功"); } }
原文链接:https://www.f2er.com/ajax/166004.html