HTTP请求参数是不可用request.getAttribute() [英] HTTP request parameters are not available by request.getAttribute()

查看:415
本文介绍了HTTP请求参数是不可用request.getAttribute()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送网​​址参数使用以下的jQuery片的servlet:

I am sending an url parameter to servlet using the following jQuery piece:

$.getJSON("http://localhost:8080/JsoupPrj/JasonGen?url=" + url, function(data) {
    $("#content").html(data);
});

在服务器端,此servlet得到的参数,对于I codeD如下:

On the server side, the servlet gets the parameter, for that I coded as below:

String url = (String) request.getAttribute("url");

但它不工作,你能告诉我哪里做错了吗?我相信我没有正确传递参数给servlet。小服务程序通过JavaScript的每次触发,但它没有看到从浏览器传递的参数。

But it is not working, can you tell me where I am doing wrong? I believe I am not passing the parameter properly to the servlet. The servlet triggers each time through the JavaScript, but it is not seeing the parameters passed from the browser.

推荐答案

在这里,

String url = (String) request.getAttribute("url");

您正在试图获得一个请求的参数作为一个请求的属性,而不是作为一个请求的参数即可。这显然​​不会做你想要的。

you're trying to get a request parameter as a request attribute instead of as a request parameter. This will obviously not do what you want.

您需要获得一个请求参数作为请求参数,而不是作为一个请求属性。

You need to get a request parameter as a request parameter, not as a request attribute.

String url = request.getParameter("url");


无关以具体的问题:你似乎并不被URL编码发送之前在所有的参数。这将有可能导致其他的问题,无关的这一个,当网​​址包含特殊字符。看JS 连接codeURIComponent()功能,或数据 $。的getJSON()功能。有关详情,请提示也如何使用servlet和Ajax?


Unrelated to the concrete problem: you don't seem to be URL-encoding the parameter at all before sending. This will possibly cause other problems, unrelated to this one, when the url contains special characters. Look at the JS encodeURIComponent() function, or the data argument of the $.getJSON() function. See for more hints also How to use Servlets and Ajax?

这篇关于HTTP请求参数是不可用request.getAttribute()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆