tags:

views:

358

answers:

11
+3  Q: 

A Java HTTP Server

I want to implement a Java HTTP server locally, I mean the server computer would be in my control. I'm expecting not more than 20 clients to send requests to it. I was wondering how to go about it:

  1. Should I use a J2EE servlet container, like Apache Tomcat?
  2. Could I use J2SE classes and just build it using them?
  3. Are there any existing alternatives?

What does your experience suggest?

+2  A: 

I guess the biggest question is: why do you want to build this?

If it is for the purpose of personal development, I'd stick to whatever standard libraries come with your JDK and build it on top of that.

If on the other hand you have a particular application that needs a dedicated HTTP server I would try to take one of the open source servlet containers, like Jetty or Tomcat and build on those.

Roland Bouman
It's not for personal use. I'm a bit confused what would be better a container based system or building on top of JDK
Kevin Boyd
I'd say, it's better if you don't have to code yourself what someone else has done already (unless you're doing it to learn from it). Jetty is light, and is used in many, many projects. Its well-known and reliable, and it does what you need. The fact that it implements a servlet container is IMO a plus. The only reason I can think of for building it yourself is if you would need something as stripped down as possible (but I am sure there is an OSS project for that too)
Roland Bouman
If I want to connect to Jetty and stay connected doing something like long polling could I do that?
Kevin Boyd
I can't say I know from experience, but I am pretty sure you can find out by reading the jetty docs and if necessary, make the appropriate modification...all in less time than coding a HTTP server from scratch yourself :) Seriously - you have the best idea of what you want to achieve, if I were you I'd be looking at the jetty site now, looking for projects that use it to get an idea if there is anything that you attempt to do.
Roland Bouman
+5  A: 

Embed Jetty in your application. Aside from performing quite well, it is delightfully easy to use and configure

Kevin
What does embedding mean? its a new term for me.
Kevin Boyd
That means that you use Jetty directly in your application instead of starting externally
Kevin
Taking the existing code and integrating it into your application.
Carl Smotricz
@Kevin I don't interpret the initial question as being about embedding anything.
Pascal Thivent
@Pascal: Was about to reply to you, but you had already deleted your post. Here is my reply. My question has 3 points and I was considering to use or implement one of those. Yes you are right about don't reinvent the wheel and don't want to do it. You have given me several solutions. Now its a problem of too many riches for me ;) Should I go in for Jetty, Grizzly or atmosphere?
Kevin Boyd
@Kevin Boyd I had suddenly a doubt about your real question but I've undeleted my answer. BTW, my comment above was for the other Kevin here :)
Pascal Thivent
+1  A: 

If you will write your own HttpServer you will have to implement all the methods of the HTTP protocol. Tomcat can be easily used locally.

Rajat
Does Tomcat support long polling in case I want to customize Tomcat for long polling can I do it?
Kevin Boyd
You don't need to customize Tomcat for long polling. That's handled in the Web application, not the application server.
Carl Smotricz
Does that mean Tomcat supports long polling?
Kevin Boyd
Every Web server "supports" long polling. You can stop worrying about it.
Carl Smotricz
+1  A: 

Is it for practice, fun, to implement special requirements or why don't you just embed an existing solution?

Andreas_D
Not for practice or fun. What are the pros/cons of a container based solution over building over JDK.
Kevin Boyd
+1  A: 

Do you really want to build a HTTP server that deals with the protocol directly, or do you just want to write web apps? If all you care about is writing the web apps, then just use Tomcat, or Jetty, or Glassfish, or another server -- it will save you a ton of work.

If you really are interested in writing your own server from scratch, then the best way would be to just use Java SE, and not use any existing server technology.

Kaleb Brasee
Basically I have some mobile clients that would be talking to me over http and requesting or sending me some data. The HTTP server has to co-ordinate between all these clients.
Kevin Boyd
+2  A: 

You've got many options, the least of which are Jetty, Grizzly, and TTiny.

I would strongly urge against writing your own web server, unless you've got time to kill and want to spend it writing things that are already available to you for free.

Jason Nichols
+1 for being concise.
Kevin Boyd
That's the kind of guy I am =)
Jason Nichols
Don't you mean *not* the least of which?
+2  A: 

Perhaps look at the list of 26 open source web servers at http://java-source.net/open-source/web-servers.

http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html is actual code in a single file implementing a multi threaded webserver. For your requirements, such as they are, this should suffice.

http://java.sun.com/developer/technicalArticles/Networking/Webserver/ is an analysis of the code.

Clint
+1  A: 

We have used this for years.

http://acme.com/java/software/Acme.Serve.Serve.html

Thorbjørn Ravn Andersen
+4  A: 

There's a simple HTTP server embedded in the Sun 1.6 JRE. It's not JavaEE or servlet-compliant, it's very lightweight, but it might be good enough for your requirements. No need to download any 3rd party stuff if this is all you need.

The javadocs, rather bizarrely, are out on their own, here.

skaffman
I also wanted to implement long polling, I read the jetty docs and it seems to have some inbuilt comet component. Here possibly I will have to implement my own stuff, unless I find some readymade component that does that.
Kevin Boyd
If you'd mentioned long-polling as a requirement, that would've narrowed the range of options drastically....
skaffman
Yeah, I should have done that in the first place, no what?
Kevin Boyd
@skaffman, I'd be interested to hear words of wisdom on Web app servers and long polling, if you have any to spare!
Carl Smotricz
I don't have any whatsoever, never mind any to spare :)
skaffman
+3  A: 

Seriously, reuse an existing solution. Why the hell are you even thinking rolling your own?

Now, 1. I don't understand your question as being about embedding a container. 2. You mentioned long polling several time. So I'd suggest to use GlassFish v3 / Grizzly (because there are many samples, e.g. have a look at the Dead Simple Comet Example on Glassfish v3 / Grizzly).

If you don't want to rely on the way a container implemented Comet support, use atmosphere and any of the container mentioned on the web site:

Atmosphere is a POJO based framework using Inversion of Control (IoC) to bring push/Comet to the masses! Finally a framework which can run on any Java based Web Server, including Google App Engine, Tomcat, Jetty, GlassFish, Weblogic, Grizzly, JBossWeb and JBoss, Resin, etc. without having to wait for Servlet 3.0 Async support or without the needs to learn how Comet support has been differently implemented by all those Containers.

If this is not a concern, just stick with the suggested option (GlassFish v3 / Grizzly).

For a concrete and recent comparison between Comet server implementation, checkout this awesome Comet Maturity comparison grid view (source: Comet Gazing: Maturity). It might help you to make your final choice... or not :)

Pascal Thivent
Well there seems good competion between GlassFish/Grizzly and Jetty...especially on this post.
Kevin Boyd
Pascal, can you verify that I didn't lie to Kevin? To me, long polling is when the client sends a request and the server purposely doesn't answer it until there's something relevant to send; and my understanding is that this is done by the app, which simply sits inactive on the open connection, while the server just treats it as a sluggishly responding app. Is there an active role for the Web server in supporting long polling?
Carl Smotricz
I've added a comparison matrix. I also suggest to google a bit to see how easy it is to find samples/support.
Pascal Thivent
@Pascal: That comparison link is fabulous, and yes so far I can't find some good comet examples for jetty or maybe I'm not looking in the right place.
Kevin Boyd