<%@ 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 ></div>
  </body>
</html>

一个有ajax功能的jsp

相关文章:

  • 2021-05-17
  • 2021-06-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-10-25
  • 2021-09-19
  • 2021-11-03
相关资源
相似解决方案