Java HTTP Server Library
I'm writing an application which needs to be able to handle HTTP requests from an external source (in this case, it's actually a script from Second Life). I started out by using HTTPComponents by Apache, which worked great as long as the Java server only had to respond to GET requests. Now it needs to be able to receive data from the body of POST requests, and I don't see any way to do so with HTTPComponents
. Is there a superior library to use for this?
I did find through some Googling that there is a basic HTTP server included in the Sun packages. For the purposes of this appli开发者_如何学Pythoncation, it could be required that it only run on an actual Sun/Oracle JVM implementation -- but that's still a code smell to me, and I'd probably only do so if the only option was to write my own HTTP server library from scratch, working up from sockets and such. Any suggestions for an alternative?
You can access the entity if the request is a HttpEntityEnclosingRequest
. There is a simple example demonstrating the use of this class.
Use Apache Tomcat. It's the standard Java web server.
If you want something minimal, NanoHTTPD is good. It uses only core java libraries and is a single file.
https://github.com/NanoHttpd/nanohttpd/blob/efb2ebf85a2b06f7c508aba9eaad5377e3a01e81/webserver/src/main/java/org/nanohttpd/webserver/SimpleWebServer.java
The disadvantage is that it doesn't seem to take advantage of NIO, which can really speed up network throughput.
精彩评论