request.getAttribute() 不提供 HTTP 请求参数 [英] HTTP request parameters are not available by request.getAttribute()

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

问题描述

我正在使用以下 jQuery 片段向 servlet 发送 url 参数:

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 获取参数,为此我编码如下:

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

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

但是它不起作用,你能告诉我我哪里做错了吗?我相信我没有正确地将参数传递给 servlet.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 编码.当 url 包含特殊字符时,这可能会导致与此无关的其他问题.查看 JS encodeURIComponent() 函数,或者 $.getJSON() 函数的 data 参数.有关更多提示,另请参阅如何使用 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?

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

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