页面ajax没显示数据库,如何在不使用Ajax刷新页面的情况下从数据库显示值到jsp...

小编典典

您必须进行以下更改:-在Servlet中:-在Servlet的response.setContentType("text/xml");顶部将响应内容类型设置为:-

。通过设置此项,我们可以以XML格式发送响应,并在JSP上检索它时,我们将基于XML的标记名来获取它。

在Servlet中执行任何您想执行的操作…将值保存为

String uname=";

uname="hello"; //some operation

//create one XML string

String sendThis="<?xml version='1.0'?>"

+""

+""

+""+uname+""

+""

+""

out.print(sendThis);

现在,我们将转到显示数据的JSP页面。

function getXMLObject() //XML OBJECT

{

var xmlHttp = false;

try {

xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers

}

catch (e) {

try {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+

}

catch (e2) {

xmlHttp = false // No Browser accepts the XMLHTTP Object then false

}

}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {

xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers

}

return xmlHttp; // Mandatory Statement returning the ajax object created

}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {

if(xmlhttp) {

xmlhttp.open("GET","NameList",true); //NameList will be the servlet name

xmlhttp.onreadystatechange = handleServerResponse;

xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xmlhttp.send(null);

}

}

function handleServerResponse() {

if (xmlhttp.readyState == 4) {

if(xmlhttp.status == 200) {

getVal();

}

else {

alert("Error during AJAX call. Please try again");

}

}

}

function getVal()

{

var xmlResp=xmlhttp.responseText;

try{

if(xmlResp.search("Maintag")>0 )

{

var x=xmlhttp.responseXML.documentElement.getElementsByTagName("Subtag");

var xx=x[0].getElementsByTagName("unameVal");

var recievedUname=xx[0].firstChild.nodeValue;

document.getElementById("message").innerText=recievedUname;//here

}

}catch(err2){

alert("Error in getting data"+err2);

}

}

至此您完成了。:)

2020-06-08

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值