简体   繁体   中英

Java SE - a small web server program

I am just curious how to implement a small server program in Java SE?

The program is listening on port 80 and ready to retrieve an InputStream object, but is there any function I can use to convert an InputStream into an HttpRequest object (something like Servlet)?

After that, how can I craft an HttpResponse object and send the data back to the browser?

Conclusively, I am just looking any framework that can help me parse the HTTP format into an object, or from an object into an HTTP response stream.

(I know Java EE will be the better solution, I just want to test something on Java SE)

Look no further than Jetty in this case. It is written for exactly that purpouse - embedding an HTTP server into your application. But probably won't get you an HttpRequest object, but perhaps something similar that should suit your needs.

If you've got the InputStream and just need to process the request/format a response, have a look at Apache HttpComponents

For more info see this answer here: https://stackoverflow.com/a/5625479/416627

Winstone是一款非常适合嵌入的轻量级servlet容器。

如果您需要Servlet实现,请查看Tomcat: http//tomcat.apache.org/

If you are looking for something very lightweight and flexible you might want to look at Netty

https://netty.io/Documentation/WebHome

Personally I found it a little hard to get started with but once the initial hurdle was overcome it was quite powerful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM