I am trying to write a simple client-server application in Java using HTTP request/response. I would like the client to be a desktop program which sends (posts) a request to a server. The server is a web page which will be hosted on Apache Tomcat server. The server must be able to read the information and display it on the browser and must be able to respond to the client with a status code 200. I am using eclipse and Apache tomcat server. I have so far tried various resources, but all I could find is a client which could request response from an already existing web server. Could someone please give me an example or some insight on how to make the client request the our own server which runs on the local machine.
Asked
Active
Viewed 147 times
0
-
Have you tried anything so far? StackOverflow is more for help with specific issues than broad "how do I start?"-type questions. – Hayden Schiff Aug 06 '15 at 15:22
-
Let me Google it for you: [Servlet tutorial](http://www.journaldev.com/1877/java-servlet-tutorial-with-examples-for-beginners), [Java Http Client Tutorial](http://www.vogella.com/tutorials/ApacheHttpClient/article.html). – Jozef Chocholacek Aug 06 '15 at 15:30
-
do you want to do it via rest web service or sockets? – j.con Aug 06 '15 at 15:33
-
I want to do it via Sockets – Qkxx Aug 06 '15 at 15:36
-
Hi, I have edited my question to be more specific. – Qkxx Aug 06 '15 at 15:37
-
http://cs.lmu.edu/~ray/notes/javanetexamples/ I doubt you want to use sockets though if you are using an existing application server such as Tomcat. You probably want a restful API approach so look at http://stackoverflow.com/questions/17381194/want-to-learn-to-build-a-simple-java-rest-server – j.con Aug 06 '15 at 15:43
1 Answers
1
Good question, though in your case, I won't recommend you implement a simple HTTP request/response approach as you will end up implementing a timer, heartbeat or Comet. You might wanna try javax or jetty WebSocket API. All you need is to create three parts:
- a websocket.server
- a websocket.client (desktop application)
- a javascript websocket client (browser agent)
Your server and both clients will become full-duplex via onMessage and send events.
Here's an example which I believe is a bit relevant one. https://dzone.com/articles/sample-java-web-socket-client

Aung Myo Linn
- 2,820
- 3
- 27
- 38