A Built-In Java HttpServer

The com.sun.net.httpserver package was introduced with JDK 1.6 and enables the implementation of embedded HTTP servers:


import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

//...

String payload = "duke";
HttpServer server = HttpServer.create(new InetSocketAddress(4250), 0);
HttpContext context = server.createContext("/java");
context.setHandler((he) -> {
	he.sendResponseHeaders(200, payload.getBytes().length);
	final OutputStream output = he.getResponseBody();
	output.write(payload.getBytes());
	output.flush();
	he.close();
});
server.start();

Test: curl -i http://localhost:4250/java returns:


HTTP/1.1 200 OK
Date: Thu, 09 Jul 2015 02:48:37 GMT
Content-length: 4

duke%    

Warning: The packages com.sun.* are not part of the supported, public interface and may even disappear in upcoming Java releases.


NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

I believe the warning should be a bit more nuanced:
- the link only talks about sun.* packages, which must indeed not be used
- com.sun.* packages/classes without @Exported annotation, or with @Exported(false), should not be used
- com.sun.* packages/classes with @Exported may be used & are likely to remain available. If they were to disappear, it would take at least 2 releases (i.e. at least 4 years): the first would still have them, but with @Exported(false), and the second would have them removed. Personally, I believe this is highly unlikely to happen for the httpserver package.

https://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/jdk/Exported.html

Posted by Anthony Vanelverdinghe on July 09, 2015 at 08:30 AM CEST #

Hi Adam

Great example but do You think it is good idea to use deprecated classes from package com.sun.* ?

What if someone use it in the project and Oracle decide to get rid of that package in new version?

Best regards
Tomek

Posted by Tomasz Janczewski on July 11, 2015 at 11:14 AM CEST #

While it's true that the Sun server may someday up and vanish, since the JDK offers JAX-WS web services out of the box, there will always be some kind of HTTP server bundled within it. So, I wouldn't count on it just up and going away any time soon.

When it was originally introduced, this HTTP server was absolutely terrible. You could not get more than 5 TPS out of it no matter what you did.

The current one is much, much better. It offers a Filter facility, and is easy enough to use for simple use cases.

Posted by Will Hartung on July 14, 2015 at 01:19 AM CEST #

Hi Adam,

I am interested in this, but I am wondering why sun http server doesn't support websocket.

Posted by Nghia on October 13, 2015 at 12:53 PM CEST #

Thank you very much.

I failed creating RESTful services using jersey and so on. Something always goes wrong. Last time Tomcat 7.0 bugged.

At least that example works. That is awesome. I have been using C# for a long time and my java knowledge is not the best.

I would be very happy if you could tell me how to pass parameters to server.createContext("/java?query="); then I could give up 'controller' classes.

Posted by BetramW on July 09, 2018 at 09:21 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License