您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關(guān)怎么在AJAX中利用Servlet處理數(shù)據(jù)并顯示,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
一、寫前臺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、設(shè)置回調(diào)函數(shù) 3、使用Open方法建立與服務(wù)器的連接 4、向服務(wù)器發(fā)送數(shù)據(jù) 5、在回調(diào)函數(shù)中針對不同響應(yīng)狀態(tài)進行處理 */ 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設(shè)置回調(diào)函數(shù) if(xmlHttp.readyState==4){ //數(shù)據(jù)接收完成并可以使用 if(xmlHttp.status==200){ //http狀態(tài)OK //5、在回調(diào)函數(shù)中針對不同響應(yīng)狀態(tài)進行處理 document.getElementById("sp").innerHTML = xmlHttp.responseText; //服務(wù)器的響應(yīng)內(nèi)容 }else{ alert("出錯:"+xmlHttp.statusText); //HTTP狀態(tài)碼對應(yīng)的文本 } } } /** //這是GET方法傳送 function getMes(){ createXMLHttpRequest(); var txt = document.getElementById("txt").value; var url="servlet/AjaxServlet?txt="+txt; url = encodeURI(url); //轉(zhuǎn)換碼后再傳輸 xmlHttp.open("GET",url,true); //3使用Open方法建立與服務(wù)器的連接 xmlHttp.onreadystatechange=showMes; xmlHttp.send(null); //4向服務(wù)器發(fā)送數(shù)據(jù) } */ /** *這是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隨機數(shù),關(guān)鍵代碼如下:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //用utf-8轉(zhuǎn)換獲得傳輸過來的碼 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(); }
看完上述內(nèi)容,你們對怎么在AJAX中利用Servlet處理數(shù)據(jù)并顯示有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。