首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从JSP中获取Servlet中的JSON对象?

如何从JSP中获取Servlet中的JSON对象?
EN

Stack Overflow用户
提问于 2010-05-04 08:25:06
回答 1查看 4K关注 0票数 0

我在JSP页面中写道:

代码语言:javascript
复制
var sel = document.getElementById("Wimax");
var ip = sel.options[sel.selectedIndex].value;
var param;
var url = 'ConfigurationServlet?ActionID=Configuration_Physical_Get';
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
httpRequest.open("POST", url, true);
httpRequest.onreadystatechange = handler(){
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
param = 'ip='+ip;
param += 'mmv='+mmv;
param += "tab="+tab;
}};
httpRequest.send(param);

我希望在我的param中使用这个ConfigurationServlet变量。有人能告诉我如何在servlet中获得这个json对象吗?

更新:我更改了语句,现在它将状态代码显示为200。

代码语言:javascript
复制
var index = document.getElementById("Wimax").selectedIndex;
var ip = document.getElementById("Wimax").options[index].text;
httpReq = GetXmlHttpObject();
alert(httpReq);
var param = "ip=" + ip; 
param += "&mmv=" + mmv; 
param += "&tab=" + tab; 
alert("param "+param);
var url="http://localhost:8080/WiMaxNM/ConfigurationServlet?ActionID=Configuration_Physical_Get";
url = url+"?"+param;
httpReq.open("GET",url,true);
alert("httpReq "+httpReq);
httpReq.onreadystatechange = handler;
httpReq.send(null);

但也出现了新的问题。控件根本不转到url中指定的servlet操作ID。请告诉我这里出了什么问题。

EN

回答 1

Stack Overflow用户

发布于 2010-05-04 11:18:52

只有在发送请求之后才会调用处理程序中的代码。在此之前,您需要填充param。您还需要通过&连接单独的参数。

因此,例如。

代码语言:javascript
复制
// ...
httpRequest.onreadystatechange = handler() {
    // Write code here which should be executed when the request state has changed.
    if (httpRequest.readyState == 4) {
        // Write code here which should be executed when the request is completed.
        if (httpRequest.status == 200) {
            // Write code here which should be executed when the request is succesful.
        }
    }
};

param = 'ip=' + ip;
param += '&mmv=' + mmv;
param += "&tab=" + tab;
httpRequest.send(param);

然后,您可以通过servlet以通常的HttpServletRequest#getParameter()方式访问它们。

尽管如此,您在那里发布的Ajax代码只会在中工作,而不会在全世界所知道的其他四个主要not浏览器中工作。换句话说,你的Javascript代码对世界上大约一半的人来说是行不通的。

我建议看一看jQuery,以减少所有冗长的工作,并消除跨浏览器兼容性问题。所有的代码都可以很容易地被替换成

代码语言:javascript
复制
var params = {
    ip: $("Wimax").val();
    mmv: mmv,
    tab: tab
};
$.post('ConfigurationServlet?ActionID=Configuration_Physical_Get', params);

并且仍然在所有的网页浏览器中工作!

更新:根据您的更新,最终的是完全错误的。?表示查询字符串的start。您的URL中已经有一个了。您应该使用&链接查询字符串中的参数。也就是说。

代码语言:javascript
复制
url = url + "&" + param;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2763805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档