zoukankan      html  css  js  c++  java
  • 一个有ajax功能的jsp


    <%@ 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>
  • 相关阅读:
    PYTHON 爬虫笔记八:利用Requests+正则表达式爬取猫眼电影top100(实战项目一)
    PYTHON 爬虫笔记七:Selenium库基础用法
    PYTHON 爬虫笔记六:PyQuery库基础用法
    java练习题(六)
    js练习题(三)
    js练习题(一)
    表格
    MAYA网络Z1J10928
    //绑定select下拉框change事件只执行一次
    枚举数组转换字符串
  • 原文地址:https://www.cnblogs.com/chinatefl/p/1181814.html
Copyright © 2011-2022 走看看