Ajax Java 交互(json)
来源:互联网 发布:moment.js fromnow 编辑:程序博客网 时间:2024/06/05 15:59
Ajax Java 交互(json)
很多朋友对于Ajax 不是很理解, 其实Ajax 并不是那些比较流行的框架(jQuery.....)。Ajax是异步的javascript 和 XML
的缩写。其实IE早就实现了异步交互的方式只是没有大规模的应用。后来GOOGLE把他用起来了后期才出现了很多像jQuery
这样的框架。现在跟大家分享一下原生态的Ajax应用。
1.编写html页面
<body><input type="button" value="click here" onclick="ajax();" /><br><input type="text" id="value1" /><input type="text" id="value2" /><div id="div"></div></body></html>
2.在jsp中编写Ajax
<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;}if (null != xmlHttpRequest){//获取HTML中的文本输入域值。var v1 = document.getElementById("value1").value;var v2 = document.getElementById("value2").value;//设置好ajax的 请求方式、地址、是否异步//xmlHttpRequest.open("GET", "AjaxServlet?v1=" + v1 + "&v2=" + v2, true);//xmlHttpRequest.send(); //真正的发送请求xmlHttpRequest.open("POST", "AjaxServlet", true);//Ajax的回调函数xmlHttpRequest.onreadystatechange = ajaxCallBack;//采用POST提交要设置请求头参数xmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");//采用POST提交xmlHttpRequest.send("v1=" + v1 + "&v2=" + v2);//真正的发送请求}}//Ajax的回调函数function ajaxCallBack(){if (xmlHttpRequest.readyState == 4){ //Ajax引擎4个阶段,4为最后一个阶段if (xmlHttpRequest.status == 200){//XMLHttpReques对象取得服务器相应信息(文本、XML)var responseText = xmlHttpRequest.responseText;//将结果写入div中//document.getElementById("div").innerHTML = responseText;//ajax解析json第一种方法eval("var json=" + responseText);document.getElementById("div").innerHTML = json.name;//ajax解析json第二种方法//var json =eval("[" + responseText + "]");//document.getElementById("div").innerHTML = json[0].name;}else{document.getElementById("div").innerHTML = "服务器错误";}}}</script>
3.编写后台Servlet
package com.ajax.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{protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{PrintWriter out = response.getWriter();// out.print("Hello World");String value1 = request.getParameter("v1");String value2 = request.getParameter("v2");out.print(value1 + value2);out.flush();}protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{PrintWriter out = response.getWriter();String value1 = request.getParameter("v1");String value2 = request.getParameter("v2");//out.print(value1 + value2);String json = "{" + '"'+ "name" + '"' +":" + '"' + value1 + '"' + "," + '"' + "age" + '"' +":" + '"' + "20"+ '"' +"}" ;out.print(json);out.flush();}}
4.一个原生态的Ajax应用编写完成运行效果
- Ajax Java 交互(json)
- 使用JSON-RPC-Java完成Ajax交互
- 使用JSON-RPC-Java完成Ajax交互
- 使用JSON-RPC-Java完成Ajax交互
- Ajax&Json交互实例
- ajax与servlet交互(通过JSON),JAVA的arraylist传到前端的方法
- java servlet使用jquery 的ajax交互 传递json对象
- php json ajax 交互示例
- Ajax与Json交互实例
- ajax+json 做数据异步交互
- Spring MVC、Ajax和JSON的交互
- 【SSH (七) 】使用ajax + json 交互
- json与ajax对服务器的交互
- springmvc + json + js + ajax 数据交互
- 2.ajax数据交互xml和json
- ajax 服务端交互返回json数据处理
- JAVA AJAX和servlet交互
- JAVA AJAX和servlet交互 .
- snmp小记
- mysql的中文乱码
- suse linux原创案例04: ssh连接反应慢问题
- Python入门教程 超详细1小时学会Python
- esx linux下出现repair filesystem解决办法
- Ajax Java 交互(json)
- OGNL, JSTL, STRUTS2标签中符号#,$,%的用法示例
- 织梦(dedecms)批量替换文章标题和内容方法集绵
- tiny6410的led裸机程序源代码
- Asp.Net 2.0 操作mysql 数据基类(读取,删除,插入,更新)
- Java基础_访问控制
- cacti故障一例
- ss
- linux系统top命令详解