先写贴一个jsp页面:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
<!--
-->
var xmlHttpRequest = null;
function ajaxSubmit(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
}
if(xmlHttpRequest!=null){
var v1 = document.getElementById("value1ID").value;
var v2 = document.getElementById("value2ID").value;
//用get方法传递 AjaxServlet 为web.xml中配置的请求
// v1="+v1+"&v2="+v2 为传递参数 如果是两个参数,别忘记写&
//xmlHttpRequest.open("GET","AjaxServlet?v1="+v1+"&v2="+v2,"true");
//xmlHttpRequest.send(null);
//一下四行是当用POST时要用到的 AjaxServlet是web.xml中配置的请求
xmlHttpRequest.open("POST"," AjaxServlet","true");
xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//POST发送
xmlHttpRequest.send("v1="+ v1 + "&v2=" + v2);//POST
xmlHttpRequest.onreadystatechange = ajaxCallback;
}
}
function ajaxCallback(){
if(xmlHttpRequest.readyState == 4){
if(xmlHttpRequest.status == 200){
var responseText = xmlHttpRequest.responseText;
document.getElementById("div1").innerHTML = responseText;
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
ajax
com.cd.servlet.AjaxServlet// AjaxServlet类的路径
ajax
/AjaxServlet
index.jsp
AjaxServlet类的代码:
package com.cd.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;
public class AjaxServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
resp.setHeader("pragma", "no-cache");//清除缓存
resp.setHeader("cache-control", "no-cache");
String v1 = req.getParameter("v1");
String v2 = req.getParameter("v2");
String v3 = String.valueOf(Integer.valueOf(v1)+Integer.valueOf(v2));
System.out.println("v1=" + v1 +" v2="+v2);
PrintWriter pw = resp.getWriter();
pw.println(v3);
pw.flush();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doPost");
doGet(req,resp);
}
}
此处不能上传源代码,我会放在我空间的其他地方,名字为ajax
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
<!--
-->
var xmlHttpRequest = null;
function ajaxSubmit(){
if(window.ActiveXObject){
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
}
if(xmlHttpRequest!=null){
var v1 = document.getElementById("value1ID").value;
var v2 = document.getElementById("value2ID").value;
//用get方法传递 AjaxServlet 为web.xml中配置的请求
// v1="+v1+"&v2="+v2 为传递参数 如果是两个参数,别忘记写&
//xmlHttpRequest.open("GET","AjaxServlet?v1="+v1+"&v2="+v2,"true");
//xmlHttpRequest.send(null);
//一下四行是当用POST时要用到的 AjaxServlet是web.xml中配置的请求
xmlHttpRequest.open("POST"," AjaxServlet","true");
xmlHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//POST发送
xmlHttpRequest.send("v1="+ v1 + "&v2=" + v2);//POST
xmlHttpRequest.onreadystatechange = ajaxCallback;
}
}
function ajaxCallback(){
if(xmlHttpRequest.readyState == 4){
if(xmlHttpRequest.status == 200){
var responseText = xmlHttpRequest.responseText;
document.getElementById("div1").innerHTML = responseText;
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
ajax
com.cd.servlet.AjaxServlet// AjaxServlet类的路径
ajax
/AjaxServlet
index.jsp
AjaxServlet类的代码:
package com.cd.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;
public class AjaxServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
resp.setHeader("pragma", "no-cache");//清除缓存
resp.setHeader("cache-control", "no-cache");
String v1 = req.getParameter("v1");
String v2 = req.getParameter("v2");
String v3 = String.valueOf(Integer.valueOf(v1)+Integer.valueOf(v2));
System.out.println("v1=" + v1 +" v2="+v2);
PrintWriter pw = resp.getWriter();
pw.println(v3);
pw.flush();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("doPost");
doGet(req,resp);
}
}
此处不能上传源代码,我会放在我空间的其他地方,名字为ajax
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26668320/viewspace-723284/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26668320/viewspace-723284/