实施Web服务器 [英] Implementation of a web-server
问题描述
我想在纯Java
中实现我自己的网络服务器,网络服务器应该只支持静态资源(即html,js,css,pics,电影等..)
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..)
你能推荐一篇关于如何实现这样的东西的教程或文章吗?
我应该使用几个进程还是一个线程池,还是应该考虑像NodeJS这样的循环事件?
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.
推荐答案
我建议熟悉您自己使用HTTP请求格式 http://datatracker.ietf.org/doc/rfc2616/。从头开始实施HTTP并不是一件容易的事,但它肯定是一个很好的学习练习。
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.
在Java本身中为简单起见我建议使用每个请求的线程服务器 - < a href =http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html =nofollow noreferrer> http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded -server.html - 使用 java.nio
来提供文件。在并发设置中 java.nio
优于 java.io
,因为它可以更好地平衡负载。您可能会发现基准测试表明 java.io
更快,但这是顺序单线程代码。
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.
这篇关于实施Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!