AJax_1

首先,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>



话说我之前还想用jsp中的java部分通过读取文件来获取文件内容变成一个字符串,然后传到js里面,发现不太对劲,好像不能这样传参的。。。从java部分跳不到js部分

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值