zoukankan      html  css  js  c++  java
  • ajax交互

    1.servlet get 

    servlet:

    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    out.write("hello ajax");
    System.out.println("ServletDemo1执行了");

    java:

    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("b1").onclick=function(){
    var xhr=createXmlHttpRequest();
    xhr.onreadystatechange=function(){
    if(xhr.readyState==4){
    if(xhr.status==200||xhr.status==304){
    var data =xhr.responseText;
    alert(data);
    document.getElementById("d1").innerHTML=data;
    }
    }
    }
    xhr.open("GET","/ajaxdayday28/servlet/ServletDemo1?time="+new Date().getTime());
    xhr.send(null);
    };
    }
    function createXmlHttpRequest(){
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Msxml2.XMLHTTP");
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Microsoft.XMLHTTP");
    }catch(e){}
    }
    }
    return xmlHttp;
    }
    </script>

    2.servlet post

    servlet:

    String username= request.getParameter("username");
    String pwd= request.getParameter("pwd");

    java:

    <script type="text/javascript">
    window.onload=function(){
    document.getElementById("b1").onclick=function(){
    var xhr=createXmlHttpRequest();
    xhr.onreadystatechange=function(){
    if(xhr.readyState==4){
    if(xhr.status==200||xhr.status==304){

    }
    }
    }
    xhr.open("POST","/ajaxdayday28/servlet/ServletDemo2?time="+new Date().getTime());
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xhr.send("username=admin&pwd=123");
    };
    }
    function createXmlHttpRequest(){
    var xmlHttp;
    try{
    xmlHttp=new XMLHttpRequest();
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Msxml2.XMLHTTP");
    }catch(e){
    try{
    xmlHttp=new ActiveXobject("Microsoft.XMLHTTP");
    }catch(e){}
    }
    }
    return xmlHttp;
    }
    </script>

  • 相关阅读:
    一封致想学J2EE的新手的回信
    C#窗体文件与HTML文本文件的相似之处
    Java 文件 获取图片文件的类型
    java 文件 目录和文件的新建、删除、复制、剪切
    java zip递归压缩解压代码
    css + div + js 制作HTML tab control
    使用命令行查看端口与进程
    浅谈php用户身份认证(四)
    日历显示程序
    php中的网页重定向――原创
  • 原文地址:https://www.cnblogs.com/zszitman/p/4500481.html
Copyright © 2011-2022 走看看