Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

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

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...