简体   繁体   English

Java SE - 一个小型Web服务器程序

[英]Java SE - a small web server program

I am just curious how to implement a small server program in Java SE? 我只是好奇如何在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)? 程序正在侦听端口80并准备检索InputStream对象,但是有没有可用于将InputStream转换为HttpRequest对象的函数(类似于Servlet)?

After that, how can I craft an HttpResponse object and send the data back to the browser? 之后,我如何制作一个HttpResponse对象并将数据发送回浏览器?

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. 最后,我只是在寻找可以帮助我将HTTP格式解析为对象,或者从对象解析为HTTP响应流的任何框架。

(I know Java EE will be the better solution, I just want to test something on Java SE) (我知道Java EE将是更好的解决方案,我只想在Java SE上测试一些东西)

Look no further than Jetty in this case. 在这种情况下,请看Jetty It is written for exactly that purpouse - embedding an HTTP server into your application. 它是为了这个purpouse编写的 - 将HTTP服务器嵌入到您的应用程序中。 But probably won't get you an HttpRequest object, but perhaps something similar that should suit your needs. 但可能不会给你一个HttpRequest对象,但也许类似的东西应该适合你的需要。

If you've got the InputStream and just need to process the request/format a response, have a look at Apache HttpComponents 如果您有InputStream并且只需要处理请求/格式化响应,请查看Apache HttpComponents

For more info see this answer here: https://stackoverflow.com/a/5625479/416627 有关详细信息,请参阅此答案: 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 如果您正在寻找非常轻量级和灵活的东西,您可能需要查看Netty

https://netty.io/Documentation/WebHome 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. 就个人而言,我发现开始时有点困难,但一旦克服了最初的障碍,它就非常强大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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