Implementation of a web-server [closed]

半腔热情 提交于 2020-01-10 11:45:10

问题


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.


回答1:


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.




回答2:


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




回答3:


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!




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/3362855/implementation-of-a-web-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!