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?
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhgfb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20