Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 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 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...