A Java HTTP Server

2019-02-07 09:04发布

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?

标签: java http
10条回答
对你真心纯属浪费
2楼-- · 2019-02-07 09:23

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

查看更多
对你真心纯属浪费
3楼-- · 2019-02-07 09:23

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 :)

查看更多
聊天终结者
4楼-- · 2019-02-07 09:25

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.

查看更多
可以哭但决不认输i
5楼-- · 2019-02-07 09:30

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.

查看更多
6楼-- · 2019-02-07 09:31

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

查看更多
Ridiculous、
7楼-- · 2019-02-07 09:35

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.

查看更多
登录 后发表回答