HTTP request parameters are not available by request.getAttribute()

痞子三分冷 提交于 2019-12-17 02:00:10

问题


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);
});

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

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

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:


Here,

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");

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?



来源:https://stackoverflow.com/questions/12112995/http-request-parameters-are-not-available-by-request-getattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!