Implementation of a web-server [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this questionI 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 exer开发者_如何学运维sice to my self.
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!
I think that this is what you want http://java.sun.com/developer/technicalArticles/Networking/Webserver/
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.
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
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.
精彩评论