AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能示例
本文實(shí)例講述了AJAX+Servlet實(shí)現(xiàn)的數(shù)據(jù)處理顯示功能。分享給大家供大家參考,具體如下:
實(shí)現(xiàn)功能:在輸入框中輸入字符,用AJAX傳到后臺Servlet處理后加上隨機(jī)數(shù),并返回到前臺顯示。
一、寫前臺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 的幾個(gè)步驟:
1、建立XmlHttpRequest對象
2、設(shè)置回調(diào)函數(shù)
3、使用Open方法建立與服務(wù)器的連接
4、向服務(wù)器發(fā)送數(shù)據(jù)
5、在回調(diào)函數(shù)中針對不同響應(yīng)狀態(tài)進(jìn)行處理
*/
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)進(jìn)行處理
document.getElementById("sp").innerHTML = xmlHttp.responseText; //服務(wù)器的響應(yīng)內(nèi)容
}else{
alert("出錯(cuò):"+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隨機(jī)數(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();
}
更多關(guān)于ajax相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jquery中Ajax用法總結(jié)》、《JavaScript中ajax操作技巧總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》及《asp.net ajax技巧總結(jié)專題》
希望本文所述對大家ajax程序設(shè)計(jì)有所幫助。
相關(guān)文章
js+ajax處理java后臺返回的json對象循環(huán)創(chuàng)建到表格的方法
這篇文章主要介紹了js+ajax處理java后臺返回的json對象循環(huán)創(chuàng)建到表格的方法,涉及javascript操作json對象動(dòng)態(tài)創(chuàng)建表格以及基于ajax與后臺交互的相關(guān)技巧,需要的朋友可以參考下2016-08-08
Ajax 表單驗(yàn)證 實(shí)現(xiàn)代碼
最近做了一個(gè)項(xiàng)目中的登錄注冊模塊,大部分功能從一個(gè)網(wǎng)站里扣出來的,部分功能自己修改,自認(rèn)為還是有點(diǎn)人性化的2009-05-05
純javascript的ajax實(shí)現(xiàn)php異步提交表單的簡單實(shí)例
下面小編就為大家?guī)硪黄僯avascript的ajax實(shí)現(xiàn)php異步提交表單的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
關(guān)于前端ajax請求的優(yōu)雅方案(http客戶端為axios)
這篇文章主要給大家介紹了關(guān)于前端ajax請求的優(yōu)雅方案,本文http客戶端為axios,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
配合AJAX天氣預(yù)報(bào)的webService 之a(chǎn)sp
配合AJAX天氣預(yù)報(bào)的webService 之a(chǎn)sp...2007-01-01
ajaxFileupload實(shí)現(xiàn)多文件上傳功能
這篇文章主要為大家詳細(xì)介紹了ajaxFileupload實(shí)現(xiàn)多文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

