HTTP request parameters are not available by reque

2019-01-02 21: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条回答
心情的温度
2楼-- · 2019-01-02 21:54

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?

查看更多
登录 后发表回答