Have you ever tried to do HTTP from within Java? I am not talking about Servlet or JSP. Just plain POJO. To do this, you can use several http libraries available. Java has a built in class called HttpURLConnection to do this.

Here is a blog that discusses using this class to send/receive HTTP requests in a Java program.

https://www.tutorialspoint.com/java/java_url_processing.htm

You can see a little more elaborate example in Alvin Alexander’s blog below.

http://alvinalexander.com/blog/post/java/how-open-url-read-contents-httpurl-connection-java

There are other libraries available. Like always, Apache has some libraries to do HTTP from within Java class. See this blog for an example:

https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

Why would you want to do Java in a POJO class? May be you want to read some URL to get the content and show parts of it it in your program. Or you are doing a Web Service (SOAP or REST) to get/post some data from a Web service.

There are other libraries, like Apache Axis2, available to do Webservices in Java as well.

Advertisements