I am currently new to Java and I would like to see an example of how someone has made a request using java code to this link http://jsonplaceholder.typicode.com/posts , and successfully displayed the response in response.jsp page.
Problem Statement
I have successfully made the call using the code below:
package app;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetClientGet {
public static void main(String[] args) {
try {
URL url = new URL("http://jsonplaceholder.typicode.com/posts");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP Error code : "
+ conn.getResponseCode());
}
InputStreamReader in = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(in);
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
} catch (Exception e) {
System.out.println("Exception in NetClientGet:- " + e);
}
}
}
and the response on the cmd is as shown below:
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 10,
"title": "optio molestias id quia eum",
"body": "quo et expedita modi cum officia vel magni\ndoloribus qui repudiandae\nvero nisi sit\nquos veniam quod sed accusamus veritatis error"
},
{
"userId": 2,
"id": 11,
"title": "et ea vero quia laudantium autem",
"body": "delectus reiciendis molestiae occaecati non minima eveniet qui voluptatibus\naccusamus in eum beatae sit\nvel qui neque voluptates ut commodi qui incidunt\nut animi commodi"
}
]
I would like to paste this data onto response.jsp page below:
//contents of response.jsp
<%@ include file="/init.jsp"%>
.
.
.
.
.
Any help would be much appreciated.
You can write Java in JSP
<%
String recieve;
String buffer;
URL jsonpage = new URL("http://jsonplaceholder.typicode.com/posts");
URLConnection urlcon = jsonpage.openConnection();
BufferedReader buffread = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
while ((recieve = buffread.readLine()) != null)
buffer += recieve;
buffread.close();
System.out.println(buffer);
%>
As you tag this question with liferay, you might want to look into creating more maintainable code and not have Java on JSPs. Instead, most likely you're going through a portlet - either rendering or serving some resource. Those are good places to place that Java code. Not the JSP.
You typically serve JSON as resource, not as part of the portlet's regular output. For resource serving, you can set the Mimetype, while the regular rendering of a portlet always generates part of the final page's HTML.
...not to forget: Keeping Java code off of your JSPs is generally a good idea, not just within Liferay. You could also implement tags, or just minimize the amount of Java by delegating to your proper Java implementations (e.g. call some methods from your JSP instead of having them implemented then and there)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With