Is there a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and manually format HTTP responses? The Java SE API nicely encapsulates the HTTP client functionality in HttpURLConnection
, but is there an analog for HTTP server functionality?
Just to be clear, the problem I have with a lot of ServerSocket
examples I've seen online is that they do their own request parsing/response formatting and error handling, which is tedious, error-prone, and not likely to be comprehensive, and I'm trying to avoid it for those reasons.
Best Solution
Since Java SE 6, there's a builtin HTTP server in
SunOracle JRE. Thecom.sun.net.httpserver
package summary outlines the involved classes and contains examples.Here's a kickoff example copypasted from their docs (to all people trying to edit it nonetheless, because it's an ugly piece of code, please don't, this is a copy paste, not mine, moreover you should never edit quotations unless they have changed in the original source). You can just copy'n'paste'n'run it on Java 6+.
Noted should be that the
response.length()
part in their example is bad, it should have beenresponse.getBytes().length
. Even then, thegetBytes()
method must explicitly specify the charset which you then specify in the response header. Alas, albeit misguiding to starters, it's after all just a basic kickoff example.Execute it and go to http://localhost:8000/test and you'll see the following response:
As to using
com.sun.*
classes, do note that this is, in contrary to what some developers think, absolutely not forbidden by the well known FAQ Why Developers Should Not Write Programs That Call 'sun' Packages. That FAQ concerns thesun.*
package (such assun.misc.BASE64Encoder
) for internal usage by the Oracle JRE (which would thus kill your application when you run it on a different JRE), not thecom.sun.*
package. Sun/Oracle also just develop software on top of the Java SE API themselves like as every other company such as Apache and so on. Usingcom.sun.*
classes is only discouraged (but not forbidden) when it concerns an implementation of a certain Java API, such as GlassFish (Java EE impl), Mojarra (JSF impl), Jersey (JAX-RS impl), etc.