这个小Demo是来区别GET与POST提交Ajax的区别.Ajax是异步的javascript与xml的调用.之前一篇博文已经介绍了Ajax的运行机制与原理.这里就直接上代码了.
1.这是我写的一个Servlet
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 Administrator
* @date 2011-3-6
* @action AjaxServlet.java
* @time 下午05:08:54
* @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, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost方式来请求");
this.process(request, response);
}
}
2.这是我写的jsp页面<script type="text/javascript"></script>中式Ajax的调用
<%@ 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 ajaxSubmit()
{
if(window.ActiveXObject) //IE浏览器
{
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest) //除IE外的其他浏览器实现
{
xmlHttpRequest = 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 ajaxCallback()
{
if(xmlHttpRequest.readyState == 4)
{
if(xmlHttpRequest.status == 200)
{
var responseText = xmlHttpRequest.responseText;
document.getElementById("div1").innerHTML=responseText;
}
}
}
</script>
</head>
<body>
<input type="button" value="get content from server" onclick="ajaxSubmit();"><br/>
<input type="text" name="value1" id="value1ID"><br/>
<input type="text" name="value2" id="value2ID"><br/>
<div id="div1"></div>
</body>
</html>
分享到:
相关推荐
AJAX POST与get提交数据的差别,喜欢了解详细内容的朋友可以下载学习!
Ajax中Get请求和Post请求的区别 简单例子 何时使用Get请求,何时使用Post请求
Java 模拟Ajax POST GET 提交代码,实测很好用。
学习ajax技术的时候,很多同学对于ajax中get与post请求总是疑问不解
Ajax Get、Post请求 Ajax Get、Post请求 Ajax Get、Post请求
在ajax中使用post方法,用常规的参数格式:param1=a1¶m2=a2 ,当参数长度过长时,依然提交不成功。比如我们经常这样写一个ajax的post请求: $.ajax({ type: "post", // post or get contentType:"application...
ajax以GET和POST方式调用WCF,带参数和不带参数的方式都有
ajax总结,load,get,post,ajax总结,还有formdata~~~
PHP+Jquery+AJAX POST,GET提交表单并接收返回
一个类实现 AJAX, 支持POST与GET提交
在jquery中ajax提交表单有post与get方式,在使用get方式时我们可以直接使用ajax 序列化表单$( 表单ID) serialize();就行了,下面我来介绍两个提交表单数据的方法。$get方式提交表单get() 方法通过远程HTTP ,下面我...
php+Ajax解决了post与get中文乱码的问题 后台页面可以结合mysql使用,以达到相应目的
本项目使用了jQuery的get/post两种方法提交Json格式的数据到后台,后台再把Json格式的数据传到前台。实现了JSON字符串和JSON对象之间的转换。
jQuery ajax、post、get、json格式传递数据例子 jQuery ajax、post、get、json格式传递数据例子
封装jquery ajax方法,方便调用,避免在代码中频繁使用 $.ajax({ type: 'GET', url: url, ...... )}; 统一调用公共方法即可,区分同步异步,get post!希望对大家有帮助
$.get()和$.post()方法 、 $.getScript()和$.getJson() 、 $.ajax()方法 、 序列化元素 、 jQuery中的Ajax全局事件 这些都有详尽的例子,一看代码,你就能明白jquery里实现ajax是怎么回事了,简单明了。
服务端模拟ajax,支持发起GET、POST、PUT、DELETE请求
详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
Ajax中POST和GET的区别
用AJAX的Get和Post调用Servlet的简单示例