首先,AJAX就是异步 Javascript和XML的简称
按我现在的理解,局部刷新原理不难,用的就是js的功能
比较核心的是内部用的XmlHttpRequest发送异步请求和回应
一般写简单AJAX有几个步骤,一个是创建XMLHttpRequest对象
然后是相对比较固定的几个句式,
xmlhttp.open()用来指定请求的地址,其实就是指定获取数据的来源。
xmlhttp.onreadystatechange用来指定执行的函数
然后就是xmlhttp.send()表示发送请求操作括号中是参数,一般为空就是null。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.io.*" import="javax.swing.*" %>
<%
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 'index.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">
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc()
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for Firefox, Opera, IE7, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",'ImbaMallLog.txt',true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
document.getElementById('myDiv').innerHTML=xmlhttp.responseText;
}
else
{
alert("Problem retrieving data:" + xmlhttp.statusText);
}
}
}
</script>
</head>
<body>
This is my JSP page. <br>
<%
JFileChooser jfc= new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
DataInputStream dis=null;
dis=new DataInputStream(new FileInputStream(file));
boolean flag=true;
String sss=new String("");
while (flag){
String str=new String("");
str=dis.readLine();
if (str==null){
flag=false;
System.out.println("break");
continue;
}
System.out.println(str);
sss+=str;
}
%>
<button type="button" οnclick="loadXMLDoc()">click</button>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
</body>
</html>