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.

1 Answer 1

0

Probably trying to do that operation into JSP is not a good idea, since you'd need to use scriptlets and I understand scriptlets are now considered not a good practice (see this)... You'd better do it into a servlet (see this)...

Anyway the code would be similar. You first need to make the request and then parse the JSON response...

  1. In order to make the request you can use HttpURLConnection class. See this question.
  2. Once you have the server response into a BufferedReader, you can parse the JSON using some library, such as Gson. You can find lots of examples of JSON deserializing using Gson in SO, like this or this.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your complete answer. I also found this other answer which uses JSTL to read and parse the JSON object. stackoverflow.com/questions/829643/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.