0

I am new to JSP and I am having trouble finding a simple concrete example to make a request to a server that returns a JSON object. What I am trying to do is something like:

myjson_object = getJSONfrom("my.webserver.com/get/json")

I basically want to add such a line to an existing JSP page so that I can have the JSON object available. I would like to avoid using AJAX or JQuery. I have found several examples like this one, but for some reason they do not work in my case.

4

1 回答 1

0

可能尝试在 JSP 中执行该操作不是一个好主意,因为您需要使用 scriptlet 并且我知道 scriptlet 现在被认为不是一个好的做法(请参阅)...您最好将其放入 servlet(看到这个)...

无论如何,代码将是相似的。您首先需要发出请求,然后解析 JSON 响应...

  1. 为了提出请求,您可以使用HttpURLConnectionclass. 看到这个问题
  2. 将服务器响应转换为BufferedReader. 后,您可以使用一些库解析 JSON,例如Gson。您可以在 SO 中找到许多使用 Gson 进行 JSON 反序列化的示例,例如thisthis
于 2013-06-27T20:46:38.183 回答