国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

AJAX+Servlet實現的數據處理顯示功能示例

2024-09-01 08:31:04
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金坛市| 肥东县| 曲沃县| 高邑县| 四平市| 临邑县| 宿州市| 华蓥市| 尉氏县| 涿鹿县| 隆尧县| 汕尾市| 商水县| 齐河县| 昌图县| 罗江县| 夹江县| 逊克县| 黎川县| 夹江县| 翁源县| 永丰县| 安平县| 普兰店市| 白河县| 团风县| 鲜城| 临江市| 宾阳县| 盐池县| 融水| 阿荣旗| 鲁山县| 木里| 定陶县| 达孜县| 托克托县| 高安市| 桂东县| 常德市| 美姑县|