㈠ 如何使用ajax獲取jsp頁面的數據
如果是在ajax提交請求的時候你需要jsp頁面數據,比如你需要獲取這個input的值你可以
使用js或者jquery通過id的方式獲取,然後放在ajax的data中提交。
㈡ jsp頁面中利用AJAX查詢資料庫
ajax的原生態方法即可,
<script type="text/javascript">
var xmlHttpRequest;
//判斷不同瀏覽器,採用不同方式創建XMLHttpRequest對象
function createXmlHttpRequest(){
if(window.ActiveXObject){
return new ActiveXObject("Microsoft.XMLHTTP");//windows瀏覽器
}else if(window.XMLHttpRequest){
return new XMLHttpRequest();//其他瀏覽器
}
}
// 發送請求到伺服器,判斷用戶名是否存在
// 請求字元串
var url = "user.do?method=doCheckUserExists&userName="+uname;
//1. 創建XMLHttpRequest組件
xmlHttpRequest = createXmlHttpRequest();
// 2. 設置回調函數
xmlHttpRequest.onreadystatechange = haoLeJiaoWo;
// 3. 初始化XMLHttpRequest組件
xmlHttpRequest.open("GET",url,true);
// 4. 發送請求
xmlHttpRequest.send(null);
}
function haoLeJiaoWo(){
if(xmlHttpRequest.readyState == 4){
if(xmlHttpRequest.status == 200){
var b = xmlHttpRequest.responseText;
alert("伺服器端返回信息:" + b);
//b 是個字元串,後台傳過來的,
//.... 你想要的操作在這里寫 動態刷新jsp頁面
}
}
}
</script>
㈢ 在jsp中獲取ajax返回的data數據
你的這個問題未必是後台數據data沒返回;
你可以在返回成功里加個判斷;
success:function(data){
if(data==null)
{
alert("not get message!");
}
else
{
alert(data);
}
}
如果接收成功則可以解析;用eval表達式或者JSON.parse(data)方法都可以,重要的是你是個多條數據,要循環獲取:
var obj[]=eval("("+data+")");
for(var i=0;i<obj.length;i++)
{
dataList.append("<tr><td align='right'>areaId</td><td>"+obj[i].areaId+"</td></tr>");
}
如果這一部也沒錯,就是你追加的地方有錯,那就自己慢慢調HTML吧!
㈣ ajax獲取jsp並解析需要怎麼做
ajax獲取jsp並解析需要先調用後台介面,交互層有db,獲取到數據後就可以直接顯示了。
舉例如下:
<html>
<head>
<title>Ajax測試頁面</title>
</head>
<script type="text/javascript">
var xmlHttpRequest;
function createXmlHttpRequest(){
if(window.ActiveXObject){ //如果是IE瀏覽器
//alert("是");
return new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){ //非IE瀏覽器
//alert("非");
return new XMLHttpRequest();
}
}
function check(){
var name=document.getElementById('uName');
var pass=document.getElementById('pass');
if(!name.value){
alert("請輸入用戶名!");
name.focus();
return;
}else if(!pass.value){
alert("請輸入密碼!");
pass.focus();
return;
}
var url="ajax.do?uName=hyw"; //這里寫上跳轉到structs的路徑和方法,並把參數傳進去
url=encodeURI(url);
url=encodeURI(url);
xmlHttpRequest=createXmlHttpRequest();
xmlHttpRequest.onreadystatechange=callBack;
xmlHttpRequest.open("POST",url,true); //true是非同步請求
xmlHttpRequest.send(null);
}
function callBack(){
var r_value=xmlHttpRequest.responseText;
var r_status=xmlHttpRequest.readyState;
alert(r_value+":::"+r_status);
}
</script>
<body>
<form action="ajax.do" method="get">
用戶名: <input type="text" size="15" name="uName"/> <br/>
密 碼: <input type="text"size="15" name="pass" /> </br>
<input type="button" value="登錄" onclick="check()"/>
</form>
</body>
</html>
structs處理的代碼:
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String uName=request.getParameter("uName");
uName=URLDecoder.decode(uName, "utf-8"); //把Ajax的傳值,轉換成utf-8
System.out.println(uName);
out.print("這就是Ajax的返回值");
㈤ 用ajax讀取資料庫里的數據,怎麼可以傳到jsp頁面呢!!
首先要將取得的數據封裝成json字元串,這個你自己寫。
大概是這樣的:
比方說User對象有name和password,
可以封裝成
String jsonStr = "{"userList":[{\"name\":\"tom\",\"password\":\"123\"},{\"name\":\"jurry\",\"password\":\"123\"}]}";
action中調用response的getWriter().println(jsonStr);
將json字元串寫會到ajax。
ajax裡面的dataType設置為「json"
然後在」success":function(data){
data.userList[0].name;
data.userList[1].password
}
就可以調用返回的json對象了
㈥ 前台JSP頁面使用Jquery的Ajax技術,如何令JSP的輸入框獲取資料庫的內容,servlet又該怎麼寫!
ajax是非同步更新,不需要刷新頁面的。 用jquery裡面的ajax。
JQUERY:
$.ajax({
type:'post',//可選get
url:'xx.do',
data:'data='dsa'',//傳給java的數據,多個參數用&連接
dataType:'text',//伺服器返回的數據類型 可選XML ,Json jsonp script html text等
success:function(msg){
//這里是ajax提交成功後,PHP程序返回的數據處理函數。msg是返回的數據,數據類型在dataType參數里定義!
},
error:function(){
ajax提交失敗的處理函數!
}
})
servert
是request.getp...
獲取的。
㈦ 在jsp頁面做的ajax查詢出來的所有數據怎麼取出這個數據中的某一個欄位的結果集
使用ajax調用的後台方法中返回一個json對象,json對象里放的是這些數據;
放的時候需要按照key:value的形式放,比如:json.put(key,value);
然後jsp頁面按照返回對象.key即可取到,由於不知道你的具體代碼,只能按照我的理解來啦
㈧ JSP+ajax從資料庫取值怎麼賦給文本框
function infordwlb(){
createXMLHttpRequest(); //調用創建XMLHttpRequest對象的方法
xmlHttp.onreadystatechange=callback; //設置回調函數
xmlHttp.open("post","select.jsp"); //向伺服器端發送請求
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
//設置發送參數
xmlHttp.send("province="+document.addform.inforclass.value);
document.form名.infordw.value=xmlHttp.responseText
}
㈨ jsp中,用ajax獲取數據
jsp中用ajax獲取數據的例子如下:
jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<HEAD>
<TITLE>留學生系統</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GBK">
<SCRIPT language=JavaScript type=text/JavaScript>
var XMLHttpReq = false;
//ajax介面
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
XMLHttpReq = new XMLHttpRequest();
}else if(window.ActiveXObject){
try{
XMLHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
}catch(e){
try{
XMLHttpReq = new ActiveXObject("Mircsoft.XMLHTTP");
}catch(e1){}
}
}
}
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange = processResponse;
XMLHttpReq.send(null);
}
function processResponse(){
if(XMLHttpReq.readyState == 4){
if(XMLHttpReq.status == 200){
var res = XMLHttpReq.responseXML.getElementsByTagName("res")[0].firstChild.data;
window.alert(res);
document.myform.userid.value="";
document.myform.pwd.value="";
}else{
window.alert("你請求的頁面有異常1");
}
}
}
function userCheck(){
var userid = document.myform.userid.value;
var pwd = document.myform.pwd.value;
if(userid == ""){
window.alert("用戶名不能為空");
document.myform.pwd.value="";
document.myform.userid.focus();
return false;
}else{
sendRequest("login?userid="+userid);
}
}
function pwdCheck(){
var pwd = document.myform.pwd.value;
var pwd2 = document.myform.pwd2.value;
if(pwd!=pwd2){
window.alert("密碼不一致");
document.myform.pwd.value="";
document.myform.pwd2.value="";
document.myform.pwd.focus();
return false;
}
}
</SCRIPT>
<LINK href="css/css.css" type=text/css rel=stylesheet>
</HEAD>
<body>
<table width="778" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" >
<tr>
<td width="17%"><img src="images/logo.jpg" width="124" height="101"></td>
<td width="558" height="101" background="images/banner.jpg"><div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="558" height="101">
<param name="movie" value="images/2.swf">
<param name="quality" value="high">
<embed src="images/2.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="558" height="101"></embed>
<param name="wmode" value="transparent">
</object>
</div></td>
<td width="11%"><table width="100%" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td height="30" class="style1"><div align="center">設為首頁</div></td>
</tr>
<tr>
<td height="30" class="style1"><div align="center">收藏本站</div></td>
</tr>
<tr>
<td height="30" class="style1"><div align="center">聯系我們</div></td>
</tr>
</table></td>
</tr>
</table>
<form method="post" action="control.jsp?action=register" name="myform">
<table width="300" border="0" align="center" bgcolor="#F0F0F0">
<tr>
<td align="center">用戶名</td>
<td><input name="userid" type="text" size="20" onblur="userCheck()"></td>
</tr>
<tr>
<td align="center">真實姓名</td>
<td><input name="username" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">性別</td>
<td>
<input type="radio" name="sex" value="0" checked="checked">男
<input type="radio" name="sex" value="1">女
</td>
</tr>
<tr>
<td align="center">密碼</td>
<td><input name="pwd" type="password" size="20"/></td>
</tr>
<tr>
<td align="center">密碼確認</td>
<td><input name="pwd2" type="password" size="20" onblur="pwdCheck()"/></td>
</tr>
<tr>
<td align="center">電子郵箱</td>
<td><input name="email" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">學校</td>
<td><input name="school" type="text" size="20"/></td>
</tr>
<tr>
<td align="center">電話號碼</td>
<td><input name="phonenum" type="text" size="20"/></td>
</tr>
<tr>
<td align="center"><img border=0 src="image.jsp"></td>
<td><input type=text name=in maxlength=4 size="8"></td>
</tr>
<tr>
<td align="center"><input type="submit" value="確定" /></td>
</tr>
</table>
</form>
</body>
</html>
㈩ 怎麼在jsp頁面的ajax裡面查詢資料庫中的某個欄位
你後台不是要查詢資料庫嗎?ajax就是前後台做數據交互的,你後台查詢出這個欄位,用ajax交互,把你查詢出的值返回到前台,要麼返回null或"",要麼返回有值的; 然後在ajax的success函數裡面寫你的邏輯就ok了;ajax知道怎麼用嗎? $.ajax({type :...