日期:2014-05-16 浏览次数:20745 次
package com.huawei.servlet; 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; /** * * @name 何枫 * @date 2011-3-23 * @action AjaxServlet.java * @time 下午11:47:06 * @package_name com.huawei.servlet * @project_name ajaxTest */ public class AjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet方式来请求"); process(request, response); } //抽取方法() private void process(HttpServletRequest request, HttpServletResponse response) throws IOException { String v1 = request.getParameter("v1"); String v2 = request.getParameter("v2"); System.out.println("v1=" + v1 +", v2=" + v2); String v3 =String.valueOf(Integer.valueOf(v1)+Integer.valueOf(v2)); PrintWriter out = response.getWriter(); // try { // Thread.sleep(5000); // } catch (InterruptedException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } //System.out.println("doGet方法调用!!!"); //清空缓存的小技巧 response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache;"); out.print(v3); out.flush(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException { System.out.println("doPost方式来请求"); this.process(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="pragma" content="no-cache; charset=ISO-8859-1"> <meta http-equiv="cache-control" content="no-cache; charset=ISO-8859-1"> <title>Insert title here</title> <script type="text/javascript"> //var xmlHttpRequest = null; //声明一个空对象以接收XMLHttpRequest对象 /* function Ajax(){ if(window.ActiveXObject) //IE浏览器 { xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest) //除IE外的其他浏览器实现 { xmlHttpRequest = new XMLHttpRequest(); } //xmlHttpRequest = window.XMLHttpRequest ? new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest() ; if(null != xmlHttpRequest) { v1 = document.getElementById("value1ID").value; v2 = document.getElementById("value2ID").value; //Servlet doGet方式来请求 //xmlHttpRequest.open("GET","AjaxServlet?v1=" + v1 + "&v2=" + v2,true); xmlHttpRequest.open("POST","AjaxServlet",true); //关联好ajax的回调函数 xmlHttpRequest.onreadystatechange = ajaxCallback; //真正向服务器端发送数据 //xmlHttpRequest.send(null); //Servlet doPost方式来请求 //用doPost提交必须加上如下一行 xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlHttpRequest.send("v1=" + v1 + "&v2=" + v2); } } */ function ajax(getorpost,url,data){ var xmlHttpRequest; if(window.ActiveXObject){ //IE浏览器 xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP"); } else{//除IE外的其他浏览器实现 xmlHttpRequest = new XMLHttpRequest(); } var statechange = function(){ if(xmlHttpRequest.readyState == 4) { if(xmlHttpRequest.status == 200) { var responseText = xmlHttpRequest.responseText; document.getElementById("div1").innerHTML=responseText; } } }; //关联好ajax的回调函数 xmlHttpRequest.onreadystatechange = statechange; i