HTTP Client-Server resquest response - java

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.

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

Related

Spring webapp display dynamic content from a database

I have a REST API made with Spring Boot. I can add entities (Persons) to a database using HTTP requests.
I would like to have a web page display those entities dynamically: whenever a new Person is stored in the database, the page displays it on top of the list. No need to refresh the page.
What are the ways to achieve this? Would JSP be enough? Do I need javascript?
The simpler the better.
What are the ways to achieve this? Would JSP be enough? Do I need
javascript?
Yes you need javascript. But you can embed it in JSP too.
This is a solved problem, you will find numerous examples in the web if you search for "websocket spring". Example https://spring.io/guides/gs/messaging-stomp-websocket/
If you are not stuck in JVM-land and RDBMS, I would encourage you to take a look at firebase-angular stack (3-way binding)
Further read
Read this https://stackoverflow.com/a/12855533/6785908
Shameless copy since posting just a link is not advisable as link may break in future
In the examples below the client is the browser and the server is the webserver hosting the website.
Before you can understand these technologies, you have to understand
classic HTTP web traffic first.
Regular HTTP:
A client requests a webpage from a server.
The server calculates the response
The server sends the response to the client.
Ajax Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at
regular intervals (e.g. 0.5 seconds).
The server calculates each response and sends it back, just like normal HTTP traffic.
Ajax Long-Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
The server does not immediately respond with the requested information but waits until there's new information available.
When there's new information available, the server responds with the new information.
The client receives the new information and immediately sends another request to the server, re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
The server sends an event to the client when there's new information available.
Real-time traffic from server to client, mostly that's what you'll need
You'll want to use a server that has an event loop
Not possible to connect with a server from another domain
If you want to read more, I found these very useful: (article), (article), (article), (tutorial).
HTML5 Websockets:
A client requests a webpage from a server using regular http (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
The server and the client can now send each other messages when new data (on either side) is available.
Real-time traffic from the server to the client and from the client to the server
You'll want to use a server that has an event loop
With WebSockets it is possible to connect with a server from another domain.
It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to
implement the client side, which is very easy!
If you want to read more, I found these very useful: (article), (article) (tutorial).
Comet:
Comet is a collection of techniques prior to HTML5 which use streaming
and long-polling to achieve real time applications. Read more on
wikipedia or this article.

Send http response to application from server using captured packets

I am trying to simulate a response from a web server login confirmation to a windows application. I have the captured packets that detail the conversation between the server and the application for successful login already as it is my application, this is for debugging and MIME simulation to test application and network security. communications create what I hope can be a custom foolproof way to prevent a MIME but I dont have a way to test it, so here I am to ask for guidance.
How would I go about simulating the response from the server to the application?
I have some idea in the direction I would possibly need to go to achieve my desired outcome:
Utilizing my network:
I have a Linux machine set up as a dynamic router, dhcp, routing, and my Linksys router just acts as an access point and ethernet switch.
1: Set up web server on Linux machine.
2: redirect traffic from application port to Linux server.
3: run server-side script to respond to application request using packets captured to establish replay successful login to server.
So, I am kinda new to using Linux tools. I have setup a Linux router, captured basic information utilizing Wireshark, and am able to program in VB, javascript, some java. I have not done much network-oriented programming other than some simple communication for authentication I have successfully established.
Any information to point me in the right direction I am grateful for!
Most logins use encryption (https / TLS) so capturing the packets won't help.
If not, the packets will form a http request, and you should be able to see the format of the request, whether the login credentials are part of the URL for GET or part of the http body for POST. It is not hard to create your own http request.
How are parameters sent in an HTTP POST request?
Each http request will be followed by an http response from the server, and the format of the headers or body will contain the login result (http requests and responses are similar in format, but the headers are not the same).
More sophisticated logins may involve a series of requests/responses.
You will need to write a simpler server to receive the requests and send the responses. Java is probably your best choice, given the languages you know, plus there will be plenty of examples online. With JS it may be possible but for the most part JS is used in browsers, so not a great choice. VB is a Windows language not supported on Linux.

Intercommunication between JNLP(client) / Apache / Java program(server)

I am practicing some web-apps technology. I setup Apache HTTP server which provides HTTP page with JNLP/FlashFX file with simple data-form to fill by the user. My first idea was to send/receive the data from JNLP with help of UDP (I just serialize object inside datagrams). To be more specific:
Apache provides a static HTTP and JNLP/FlashFX (HTTP is just to deploy JNLP)
JNLP communicates via UDP with server
server runs simple Java program to send/receive UDP packets to JNLP
My problem is when I access the page from 'client' browser machine a firewall asking if I want to allow/deny the access to network from 'java'. No doubts this is normal, but I think no one is expecting this from a web page.... I want to change this approach and use existing HTTP protocol.
QUESTION UPDATE
As far as I understood with HTTP protocol we have couple of methods which are used to communicate with the server (GET, PUT, POST .... ) and provided by Apache service.
I would like to use this for data exchange like this:
JNLP sends some serialized data with HTTP methods
Apache will redirect some (or full) traffic to my Java program
My Java program will answers via Apache to my JNLP
How can I do that?

Java web server to process Android client request

Since I'm a newbie to Java web server, I don't know where to begin to create a server in Java to reply from Android devices request( A WEB SERVER NOT AN ACTUAL SITE).
Which technology should I use and which document should i read?. I have searched on the Internet about Java but i found out many technologies and i don't know which one i can use.
Spring MVC,JSF,JSP,Struts,Servlet..............
Hope this question clear enough for you to reply. Thank you
It doesn't matter what be process android devices requests on server side. In this case main idea is http communication - client sends request, server process this request and sends response to client, and you can use any technology on server side - java, php etc.
If you prefer java I can suggest you try servlets, it is simplest technology of those you mentioned above.

writing desktop application for connecting with server

I'm working on a server client base "Desktop Application". And what I want to do is this,
Client application sending some request code to server through internet(oh yes, these connections must be secured). and when server application gets this code it will again do some work and gather some data.
And then server application will return those data again to the client application.
As I am new in this, I need to know, what kind of tools to use?
Is eclipse good for that?
Where can I find good examples for writing client?
Where can I find examples for connecting client with server side?
I'd recomend checking out apache httpclient it really helps when dealing with web APIs.

Categories

Resources