could you please give me a sample code on how an Http Server(Java) receives the request of a client(android)? I mean the client sends the request via Httppost, how the server takes the content of these requests in order to see the context and reply? I am trying to built a chat application.
Thank you in advance!
The server-side of HTTP is usually implemented using the protocol stack provided by a web container. You would then implement your application's server-side as servlets. There are numerous tutorials on this.
If that's the way you want to proceed, look at one of the standard web containers; e.g. Tomcat, Jetty, Glassfish, etc. The source code for all of these is available for you to browse, though I should warn you that they are all complicated under the hood.
Assuming that your HTTP service is going to be delivering JSON or XML (rather than HTML) to clients, you may want to look into using a RESTful framework.
Have a look at ServerSocket. Keep in mind that accept() blocks and, as you will probably run it in a service, you will want to time it out and check for the completion of the service. That should probably run in its own thread as should the responders to requests.
From there, you can open input and output streams to receive the request and write the response. There are any number of packages that can help you with the interaction, or you can roll your own, but it doesn't seem like you've done a lot of homework. Perhaps some searching, reading, and more specific questions would more you along more quickly.
Related
I have a client software that is written in C++/C# and a database. Now I don't want the client to access the database directly, so I thought about placing an application server in the middle. This one should get a short request from the client, ask the database for new data, do some filtering (that can't be done in sql) and then return the data to the client.
My search for this kind of software brought me to Glassfish or Tomcat but my problem in understanding is, that these always want to talk http with html/jsp. Because most of my data is encrypted anyways, I don't need such plain text protocols and would be totally happy with something that just takes a byte stream.
On the other hand would it be nice to have a server handle the thread pool for me (don't want to implement all that from scratch).
After more than a day of searching / testing I'm even more confused than at the beginning (ejb, beans, servlet, websocket, ... so many things to google before understanding just the simplest tutorials).
TL;DR: how do I get Tomcat/Glassfish to just open a socket and create a new thread for every request, without any HTML/CSS/JSP involved?
Jetty and Tomcat are so called servlet container and thus primarly targeted at HTTP exchanges. Glassfish is an application server that uses a servlet container as one of its modules. I would stop thinking in that direction - that's all more like web applications and web services - some levels too high what you are asking for.
I think you should more look into sth. like Netty which is merley a "high performance protocol" server. Take a look at the documentation here (even some sort of tutorial there which might fit your use case).
GlassFish is an "enterprise application server", targeting the Java EJB specification. That's surely overdone for your purpose. You can give Tomcat a try. It is a "servlet container", targeting Java Servlet specification. Servlets have one purpose: listening to an incoming URL (request), executing Java code and returning a response, usually over HTTP.
For sure, you may start your own (plain) ServerSocket, for example using a ServletContextListener (which will be started once your application starts). But you should go for a higher protocol to send the data, like Hessian and Burlap, which is implemented in both, Java and C++ and easy to set up.
We have one customer who sends us thousands of web service requests per minute, and what we're seeing with all of their requests specifically is that the HTTP body comes across the wire, then there is a 150-250ms pause, and then the SOAP body is sent.
Being that the header knows the size of the request, I can rule out that this delay is caused by processing needed to generate the request itself.
Based on the user agent, I can determine that they're using Java 1.5, with HttpsUrlConnection. I don't have access to their code (yet), but I'm wondering if people have seen this behavior or not with poorly written code?
My side: A set of Apache web servers, that are front-ended with load balancers, firewalls, ... those web services take requests and use mod_proxy to send them back to Tomcat application servers.
(Again, this behavior is only seen with this one client, so I have doubts that something on my side is causing this...)
Ended up being a router on my customer's edge was completely saturated/overloaded, adding 400ms+ of latency.. So looks like something where the complete request couldn't be sent quick enough. We have reproduced this same behavior adding 400ms of latency with tcpproxy.
Sometimes it's the most obvious explanation, I guess...
I've got a news server in Java and want to make it possible for clients to receive news as soon as they appear in database, without reloading client's page. For this purpose I decided to make HTTP request from client that returns response only after news become available. But if there are a lot of clients, server won't be able to accept new requests. So, is there any java technology that deals with it?
P.S. news server is just a similar model, but not an exactly problem, so, please, think about it more abstractedly=)
The term you're looking for is push communication. You could have a look at Comet, or the Java API for WebSockets.
I am looking for advice on how to go about writing a small and simple application that will receive http GET and http PUT request, process the data (simple text files) and respond.
I have all ready done this using threads and sockets but there must be a simpler and more efficient way. Also when I run my application using wireshark I am not convince I am using the http protocol as I should be.
Thanks
Alexis
You can use Tiny Java Web Server. (http://tjws.sourceforge.net/)
Alternatively, if you are using Java 6 or later, you can use the Http server API.
I used HttpComponents for similar purposes - it provides functionality for HTTP server and client parts implementation. It's easy to learn and use.
You can consider embedding a webserver like Jetty (start/stop it from java app) if you want to get the full benefits of HTTP parsing.
I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site.
I have got the java code receiving connections on sockets and responding with some simple HTML.
My problem is that I will POST data to the Java server and that code needs to act on the data. How do I go about reading the data that is posted in the HTML request, if it is even possible. If not, is there any other way you would do this.
If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing.
Thanks
You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple
and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod
In your case I probably wouldn't work with low level socket connections.
I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. With a servlet you can easily access the POST data, process it and return something to the caller.
If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container.
If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. This allows you to provide a fully featured API for your Java resources in an easy way.