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>
  • 相关阅读:
    November 07th, 2017 Week 45th Tuesday
    November 06th, 2017 Week 45th Monday
    November 05th, 2017 Week 45th Sunday
    November 04th, 2017 Week 44th Saturday
    November 03rd, 2017 Week 44th Friday
    Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )
    图片方向 image orientation Exif
    Asp.net core 学习笔记 ( Router 路由 )
    Asp.net core 学习笔记 ( Configuration 配置 )
    qrcode render 二维码扫描读取
  • 原文地址:https://www.cnblogs.com/chinatefl/p/1181814.html
Copyright © 2011-2022 走看看