白桦的天空

第一次的心动,永远的心痛!
  首页  :: 新随笔  :: 联系 :: 管理

一个有ajax功能的jsp

Posted on 2008-05-04 14:57  白桦的天空  阅读(629)  评论(0编辑  收藏  举报


<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
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">
   
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>
  Get:<br>
  <form action="servlet/mytest" method="get" name="form1">
<input type="text" name="key" value="中国">
<INPUT type="submit" value="提交">
</form>
<br>
Post:<br>
  <form action="servlet/mytest" method="post" name="form2">
<input type="text" name="key" value="中国人">
<INPUT type="submit" value="提交">
</form>
<%
for(int i=0;i<5;i++)
{
out.println(i + "回车");
}
%>
<input type="button" onclick="ajaxTest()" value="AJAX测试">

<input type="button" onclick=" document.getElementById('weather').innerHTML='' " value="隐藏">

<script>
// 1. 初始化 请求 对象
// Mozilla/Firefox 下的
//var xmlhttp = new XMLHttpRequest();
// IE 下的
 var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
//alert(xmlhttp);

function ajaxTest() {
    // 指定要打开的页面
    xmlhttp.open("GET", "servlet/mytest", true);// HTTP 请求的方式, URL, 是否异步
    // 指定页面打开完之后要进行的操作.
    xmlhttp.onreadystatechange = mychange;
    // 开始发起浏览请求, Mozilla 必须加 null
    xmlhttp.send(null);
}

// 这个函数就是每次状态改变要调用的函数
function mychange() {
    // 请求已完成
    if(xmlhttp.readyState == 4) {
        //alert(xmlhttp.readyState);
        alert(xmlhttp.responseText);
        // 更新对应的 HTML 元素里面显示的内容
    // 根据 ID 引用页面里面的元素 document.getElementById(元素名)
    document.getElementById('weather').innerHTML = xmlhttp.responseText;
    }
}
</script>

<div id="weather"></div>
  </body>
</html>


<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
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">
    
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->
  
</head>
  
  
<body>
  Get:
<br>
  
<form action="servlet/mytest" method="get" name="form1">
<input type="text" name="key" value="中国">
<INPUT type="submit" value="提交">
</form>
<br>
Post:
<br>
  
<form action="servlet/mytest" method="post" name="form2">
<input type="text" name="key" value="中国人">
<INPUT type="submit" value="提交">
</form>
<%
for(int i=0;i<5;i++)
{
out.println(i 
+ "回车");
}
%>
<input type="button" onclick="ajaxTest()" value="AJAX测试">

<input type="button" onclick=" document.getElementById('weather').innerHTML='' " value="隐藏">

<script>
// 1. 初始化 请求 对象
//
 Mozilla/Firefox 下的
//
var xmlhttp = new XMLHttpRequest();
//
 IE 下的
 var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
//alert(xmlhttp);

function ajaxTest() {
    
// 指定要打开的页面
    xmlhttp.open("GET""servlet/mytest"true);// HTTP 请求的方式, URL, 是否异步
    // 指定页面打开完之后要进行的操作.
    xmlhttp.onreadystatechange = mychange;
    
// 开始发起浏览请求, Mozilla 必须加 null
    xmlhttp.send(null);
}


// 这个函数就是每次状态改变要调用的函数
function mychange() {
    
// 请求已完成
    if(xmlhttp.readyState == 4{
        
//alert(xmlhttp.readyState);
        alert(xmlhttp.responseText);
        
// 更新对应的 HTML 元素里面显示的内容
    // 根据 ID 引用页面里面的元素 document.getElementById(元素名)
    document.getElementById('weather').innerHTML = xmlhttp.responseText;
    }

}

</script>

<div id="weather"></div>
  
</body>
</html>