• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

request.getAttribute() 不提供 HTTP 请求参数

用户头像
it1352
帮助1

问题说明

我正在使用以下 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.

正确答案

#1

这里,

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?

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhgfb
系列文章
更多 icon
同类精品
更多 icon
继续加载