views:

119

answers:

5

I had like to implement my own web-server in pure Java the web-server should support only static resources (i.e. html, js, css, pics, movies etc..)

Can you recommend a tutorial or an article on how to implement such a thing? should I use few processes or a thread-pool or should I consider a loop-event oriented like NodeJS?

I know there are free web-servers that does exactly what I am looking for, but I had like to do it as an exersice to my self.

+3  A: 

I think that this is what you want http://java.sun.com/developer/technicalArticles/Networking/Webserver/

zoomer.hammerball
why multithreading is the right approch?
WSimpleGuy
Depends on traffic and the purpose of the web server, if you're going to serve only static pages, i think that it is the simpler and better approach. If you're going to use comet or you're going to have a lot off concurrent traffic event driven is better, you don't need one thread per request blocked while the connection is open.
zoomer.hammerball
+4  A: 

If you're doing this as an exercise, I'd recommend an event-driven model.

I don't think there's one tutorial on this topic because the knowledge required is so far-ranging - the HTTP protocol, file access, threading and concurrency, configuration-file management, socket communication, logging, error handling, MIME types... Yeah, even just sharing static resources, it's still a biggie.

Read up, and good luck!

Borealid
+1  A: 

I recommend familiarizing yourself with the HTTP request format http://datatracker.ietf.org/doc/rfc2616/. Implementing HTTP from scratch is no small feat, but it is certainly a good learning exercise.

Within Java itself for simplicity I recommend using a thread-per-request server - http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html - that using java.nio for serving files. In a concurrent setting java.nio is preferable to java.io because it balances load better. You will likely find benchmarks that suggest that java.io is faster, but that is for sequential single-threaded code.

Alain O'Dea
Might be better to try java.nio2 if you don't mind using the JDK 7 early access.
Steve-o
+1  A: 

You might find the ACME web server interesting as a starting point. We use it for ad-hoc file transfers. When you have familarized yourself with it, you can see if you can discover its bottlenecks and then ponder on how to fix it :)

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

Thorbjørn Ravn Andersen
A: 

I would suggest Apache MINA to do the job. It lets you specify Encoders/Decoders to deal with the HTT Protocol properly and calls a method when a request arrives etc. It handles thread management internally. I never start a networking project without it.

f1sh