HTTP server API in Sun's Java SE 6

Sun's implementation of Java SE 6 includes a light-weight HTTP server API and implementation. This is used internally by other components of Java SE, but it can just as easily be used for user applications to deploy simple HTTP server applications. The relevant API packages are: The first of these contains the API itself, and the second contains an SPI (Service Provider Interface) which allows alternative implementations to be plugged in. You are more likely to be interested in the first package.

You are probably wondering already about the package names, and why they are com.sun.httpserver rather than something like java.net.httpserver. What this distinction means, is that the API and implementation are a fully supported, publicly accessible component of Sun's implementation of Java SE 6. It does mean however, that the packages are not formally part of the Java SE platform, and are therefore not guaranteed to be available on all other (non Sun) implementations of Java SE 6.

So, what can you do with the HTTP server ?

First, the package allows programmers to create simple embedded http and https applications. The API is similar in some respects to the servlet API in Java EE, but this API is more light-weight and does not provide the facilities/services that the servlet container environment provides.

It does have some innovations like a pluggable thread model. What this means is that the server does not create threads for handling incoming Http requests. Instead, the management of threads is delegated to an Executor object provided by the application. A number of off the shelf Executor implementations are provided in Java SE 6, and the programmer can choose one (or roll your own).

The following table shows the main classes and interfaces that a programmer needs to know about for programming a HTTP server.

Class/interfaceWhat does it do?
HttpServer Listens on a particular port number and address for incoming Http requests
HttpsServer Same as for HttpServer except for Https requests
HttpHandler Programmer implements this. Handler is called to handle each incoming Http request
HttpExchange Encapsulates all of the information about a Http request and the corresponding response
HttpContext Represents one Http application on a HttpServer instance. It is identified by the URI path to the application, and has a HttpHandler associated

There are a number of other auxiliary classes in addition to the ones mentioned above. They provide services like basic http authentication and a filtering mechanism, that can be applied to all incoming requests.

So that is a very brief overview of the Http server API. I plan to augment this with some code samples of how to use the API, how to configure the use of SSL etc.

Comments:

Hi Michael, This is very useful information. I wonder if you could give an example of setting up the HttpsServer to run over SSL? Thx, Kaiwen

Posted by Kaiwen on December 26, 2006 at 03:03 AM PST #

The links to javadocs seem to be broken. Here are correct links: The package description contains an example of a HTTPS server.

Posted by Jari Mustonen on January 02, 2007 at 01:15 AM PST #

I was wondering if you or someone else will eventually provide documentation on how to use Tomcat's or Jetty's servlet container in conjunction with this embedded HttpServer, or if Sun will eventually be providing a route to using Servlets with this HttpServer implementation.

Posted by Ravi Luthra on January 05, 2007 at 03:29 AM PST #

Hi Michael, the introduction is good and useful. Can you or someone else tell me, is this some kind effort from sun towards building small embeddable http servers like SHTTPD or Xitami ?

Posted by Pavan Kumar on February 20, 2007 at 07:36 PM PST #

Wow, just in time ! I was using Jetty as embedded HTTP server for Java 6 application and will surely switch to this one. Thanks a lot for the info !

Posted by Evgeny Goldin on March 01, 2007 at 07:14 AM PST #

I don't think I'll be switching from Jetty since it does not depend on a Sun JRE.

Posted by Alastair Rae on March 05, 2007 at 06:06 PM PST #

s

Posted by guest on May 20, 2007 at 11:35 PM PDT #

Haven't looked at this in details, but i am assuming that the server uses Java nio for its IO implementations

Posted by Jerry on July 03, 2007 at 03:17 PM PDT #

[Trackback] We’ve recently been forced to go back to Java 5. After a massive refactoring task to remove all @Override annotations on method implementations, we were only left with one thing missing… SUN’s HTTP Server API (a.k.a. com.sun.net.https...

Posted by Javablog on October 30, 2007 at 04:00 AM PDT #

[Trackback] We’ve recently been forced to go back to Java 5. After a massive refactoring task to remove all @Override annotations on method implementations, we were only left with one thing missing… SUN’s HTTP Server API (a.k.a. com.sun.net.https...

Posted by Javablog on October 30, 2007 at 04:00 AM PDT #

we are using standalone jax-ws, we have a serious connections leak with lightweight http server

see dragonjoke.blogspot.com

Posted by Ken on June 28, 2008 at 02:33 AM PDT #

good

Posted by guest on September 17, 2010 at 08:21 PM PDT #

[Trackback] Example source code illustrating use of the HTTP server included in Java JDK 1.6. via Groovy to present a browser-based UI to a local application....

Posted by Josef's Blog on April 18, 2011 at 08:01 PM PDT #

[Trackback] Example source code illustrating use of the HTTP server included in Java JDK 1.6. via Groovy to present a browser-based UI to a local application....

Posted by Josef's Blog on April 18, 2011 at 08:10 PM PDT #

[Trackback] Example source code illustrating use of the HTTP server included in Java JDK 1.6. via Groovy to present a browser-based UI to a local application....

Posted by Josef's Blog on April 18, 2011 at 09:13 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

michaelmcmahon

Search

Top Tags
Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today