simple HTTP server in Java using only Java SE API

前端 未结 17 1718
无人共我
无人共我 2020-11-22 13:28

Is there a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and man

17条回答
  •  悲哀的现实
    2020-11-22 14:12

    I like this question because this is an area where there's continuous innovation and there's always a need to have a light server especially when talking about embedded servers in small(er) devices. I think answers fall into two broad groups.

    1. Thin-server: server-up static content with minimal processing, context or session processing.
    2. Small-server: ostensibly a has many httpD-like server qualities with as small a footprint as you can get away with.

    While I might consider HTTP libraries like: Jetty, Apache Http Components, Netty and others to be more like a raw HTTP processing facilities. The labelling is very subjective, and depends on the kinds of thing you've been call-on to deliver for small-sites. I make this distinction in the spirit of the question, particularly the remark about...

    • "...without writing code to manually parse HTTP requests and manually format HTTP responses..."

    These raw tools let you do that (as described in other answers). They don't really lend themselves to a ready-set-go style of making a light, embedded or mini-server. A mini-server is something that can give you similar functionality to a full-function web server (like say, Tomcat) without bells and whistles, low volume, good performance 99% of the time. A thin-server seems closer to the original phrasing just a bit more than raw perhaps with a limited subset functionality, enough to make you look good 90% of the time. My idea of raw would be makes me look good 75% - 89% of the time without extra design and coding. I think if/when you reach the level of WAR files, we've left the "small" for bonsi servers that looks like everything a big server does smaller.

    Thin-server options

    • Grizzly
    • UniRest (multiple-languages)
    • NanoHTTPD (just one file)

    Mini-server options:

    • Spark Java ... Good things are possible with lots of helper constructs like Filters, Templates, etc.
    • MadVoc ... aims to be bonsai and could well be such ;-)

    Among the other things to consider, I'd include authentication, validation, internationalisation, using something like FreeMaker or other template tool to render page output. Otherwise managing HTML editing and parameterisation is likely to make working with HTTP look like noughts-n-crosses. Naturally it all depends on how flexible you need to be. If it's a menu-driven FAX machine it can be very simple. The more interactions, the 'thicker' your framework needs to be. Good question, good luck!

提交回复
热议问题