0

This is somewhat similar to a previous posting but focuses on a specific aspect because I really to know the answer to this specific question before I can even move forward at this point. I need to send information in the form of a JSON object from a Java SE application to a Java EE web application. The Java EE web application is running from NetBeans in GlassFish. Both the Java SE and Java EE applications are running on the same computer.

What would be the best means to communicate between the two applications considering that they are both on the same computer: a socket connection, an http post request connection, or a combination of socket and http post request connection?

I would really appreciate any responses.

Daron
  • 329
  • 1
  • 4
  • 24
  • HTTP needs sockets, so any HTTP connection is also a socket connection, but not any socket connection a HTTP connection. Thus, HTTP introduces more overhead. – Steffen Ullrich Dec 06 '14 at 14:36
  • By the way thank you very much for the response. Would a socket connection connection do what I need to do in the scenario described above? – Daron Dec 06 '14 at 14:40
  • A socket connection can be used to transport any byte stream, same as HTTP. In both cases you need to serialize the object. – Steffen Ullrich Dec 06 '14 at 15:43

1 Answers1

1

Choosing between a self implemented socket or use HTTP if you are already using Glassfish is a no-brainer: Use HTTP.

Glassfish has already proved and tested code to talk to clients via a socket connection, implemented the HTTP protocoll, uses thread pools, etc. The only thing you have to do is to implement a small Servlet class abstracting all the complicated transportation details away.

On client side you can use a library like Apache HTTP client.

Sure you can use plain sockets. But do you want to implement your own protocol? Why? And then it might work for client and server on the same computer. But maybe later you want to use a connection over the internet with proxy server or a firewall between. With HTTP using the right libraries this is no problem.

vanje
  • 10,180
  • 2
  • 31
  • 47
  • vanje...will the HTTP Post request allow me to communicate within the same computer? Also, on the Java EE side how many places will I put the code? Will I put the code in just one servlet. Or will I have to code the xml file or any other places? – Daron Dec 07 '14 at 00:03
  • Yes, it will work within the same computer. Use the loopback interface 127.0.0.1 (localhost). You can write your servlet without the need to configure any XML file. Look here http://stackoverflow.com/questions/5031018/how-to-invoke-a-servlet-without-mapping-in-web-xml – vanje Dec 07 '14 at 00:54
  • I'm still looking through the information you sent me and thank you so much for taking the time to help me. This is so important to me. Two final questions: Does it matter where that servlet is placed and does the Apache HTTP Client jar need to be placed in both the Java SE and Java EE applications? – Daron Dec 07 '14 at 02:09
  • You create the servlet in your web application running in Glassfish. With the path in the `@WebServlet' annotation you determine the URL to connect to your servlet. The Apache HTTP client jars are only needed for your Java SE application. For your Java EE application you have Glassfish for the server side of HTTP. There is an official Java EE tutorial from Oracle with all the details about servlets and Java web applications: http://docs.oracle.com/javaee/6/tutorial/doc/ – vanje Dec 07 '14 at 14:32