欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AJAX+Servlet实现的数据处理显示功能示例

程序员文章站 2022-05-14 22:28:16
本文实例讲述了ajax+servlet实现的数据处理显示功能。分享给大家供大家参考,具体如下: 实现功能:在输入框中输入字符,用ajax传到后台servlet处理后加上随...

本文实例讲述了ajax+servlet实现的数据处理显示功能。分享给大家供大家参考,具体如下:

实现功能:在输入框中输入字符,用ajax传到后台servlet处理后加上随机数,并返回到前台显示。

一、写前台jsp页面index.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
  <title>my jsp 'index.jsp' starting page</title>
  <script type="text/javascript">
  /*
    ajax 的几个步骤:
    1、建立xmlhttprequest对象
    2、设置回调函数
    3、使用open方法建立与服务器的连接
    4、向服务器发送数据
    5、在回调函数中针对不同响应状态进行处理
  */
    var xmlhttp;
    function createxmlhttprequest(){  //1建立xmlhttprequest对象
      if(window.activexobject){
        try{
          xmlhttp = new activexobject("microsoft.xmlhttp");
        }catch(e){
          alert("error!!!");
        }
      }else{
        xmlhttp = new xmlhttprequest();
      }
    }
    function showmes(){   //2设置回调函数
      if(xmlhttp.readystate==4){ //数据接收完成并可以使用
        if(xmlhttp.status==200){ //http状态ok
        //5、在回调函数中针对不同响应状态进行处理
          document.getelementbyid("sp").innerhtml = xmlhttp.responsetext; //服务器的响应内容
        }else{
          alert("出错:"+xmlhttp.statustext); //http状态码对应的文本
        }
      }
    }
    /**
    //这是get方法传送
    function getmes(){
      createxmlhttprequest();
      var txt = document.getelementbyid("txt").value;
      var url="servlet/ajaxservlet?txt="+txt;
      url = encodeuri(url); //转换码后再传输
      xmlhttp.open("get",url,true); //3使用open方法建立与服务器的连接
      xmlhttp.onreadystatechange=showmes;
      xmlhttp.send(null); //4向服务器发送数据
    }
    */
    /**
    *这是post方法
    */
    function postmes(){
      createxmlhttprequest();
      var txt = document.getelementbyid("txt").value;
      var url = "servlet/ajaxservlet";
      var params = "username="+txt;
    // alert(params);
      xmlhttp.open("post",url,true);
      xmlhttp.setrequestheader("content-type","application/x-www-form-urlencoded;charset=utf-8");
      xmlhttp.send(params);
      xmlhttp.onreadystatechange = showmes;
    }
  </script>
 </head>
 <body>
  <input type="text" id="txt"/>
  <input type="button" value="query" onclick="postmes()" />
  <span id="sp"></span>
 </body>
</html>

二、写后台servlet加random随机数,关键代码如下:

public void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    request.setcharacterencoding("utf-8"); //用utf-8转换获得传输过来的码
    response.setcontenttype("text/html");
    printwriter out = response.getwriter();
    string txt = request.getparameter("txt");
//   string tx = new string(txt.getbytes("iso-8859"),"utf-8");
    out.print("txt="+txt+math.random());
    out.flush();
    out.close();
}
/**
* the dopost method of the servlet. <br>
*
* this method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws servletexception if an error occurred
* @throws ioexception if an error occurred
*/
public void dopost(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    request.setcharacterencoding("utf-8");
    response.setcontenttype("text/html;charset=utf-8");
    printwriter out = response.getwriter();
    string username = request.getparameter("username");
//   string txt = new string(username.getbytes("iso-8859-1"),"utf-8");
    string txt = new string(username);
    out.print("txt="+txt+":"+math.random());
    out.flush();
    out.close();
}

更多关于ajax相关内容感兴趣的读者可查看本站专题:《jquery中ajax用法总结》、《javascript中ajax操作技巧总结》、《php+ajax技巧与应用小结》及《asp.net ajax技巧总结专题

希望本文所述对大家ajax程序设计有所帮助。