Servlet容器中的Java Web应用程序与独立版本

Servlet容器中的Java Web应用程序与独立版本

问题描述:

构建一个小型Java Web应用程序以在Servlet容器(如Tomcat)中运行与构建具有内置Web服务器的独立Java应用程序并在反向代理服务器后面运行它有什么好处?

What are the advantages of building a small Java web app to run in a Servlet container (like Tomcat) vs. building a standalone Java app with a built-in web server and running it behind a reverse proxy?

我已经玩Java大约一年了。我注意到启动Tomcat需要时间,并且由于类加载器问题,并不总是可以进行热重新部署。对我来说,Servlet API似乎有点令人费解,特别是从配置和RESTful设计的角度来看(它并不完全支持)。

I've been playing with Java for about a year. I've noticed that to launch Tomcat takes time, and it's not always possible to do a hot-redeploy due to class loader issues. The Servlet API seems somewhat convoluted to me, especially from the perspective of configuration and of RESTful design (which it doesn't really fully support).

另一方面,我注意到我的IDE可以快速编译和运行一个独立的应用程序。配置Apache进行反向代理是一件小事,嵌入式Jetty似乎可以处理任何我可以抛出的内容。当我可以使用Restlet,Wicket等时,我不需要Servlet。能够更好地了解我的应用程序是如何工作的(因为它没有与庞大的应用程序服务器集成)让人感觉更有能力。堆栈跟踪更短。下载大小更小。最终用户配置更容易。我猜测性能可能会更好,因为涉及的软件层较少。

On the other hand, I've noticed that my IDE can compile and run a standalone app at lightning speed. Configuring Apache for reverse-proxying is a piece of cake, and embedded Jetty seems to handle whatever I can throw at it. I don't need Servlets when I can use Restlet, Wicket, etc. Being able to know better how my app works (because it's not integrated with a huge app server) feels empowering. The stack traces are shorter. Download size is smaller. End-user configuration is easier. I'm guessing performance is likely to be better because there are fewer software layers involved.

然而,我想起了一个说法听起来好得令人难以置信的说法是。所以我的问题是,为什么我不想让我的网络应用程序独立?什么是Servlet容器给我和/或我的最终用户我们真正需要但不知道的?

However, I am reminded of the saying that what sounds too good to be true usually is. So my question is, why would I not want to make my web apps standalone? What does a Servlet container give me and/or my end users that we really need but don't know about?

那里这里有两个单独的问题:

There are 2 separate questions in here:


  1. 我应该使用嵌入式
    服务器,还是部署到容器中?

  1. Should I be using an embedded server, or deploy into a container?

我认为你不应该以某种方式看到
的巨大差异。
以编程方式为
启动Jetty服务器
的代码略多,配置
更容易以编程方式执行。
尽管IDE支持Web应用程序
配置和部署是
越来越好,但它仍然比独立应用程序
更糟糕
(这有点像定义,因为
有一个超集的东西到
支持。)

I don't think you should be seeing a big difference one way or the other. There's slightly more code to startup a Jetty server programmatically, and configuration is easier to do programmatically. Even though IDE support for web app configuration and deployment is getting better, it's still worse than for standalone applications (this is kinda by definitions, since there's a superset of things to support).

另一方面,应用服务器给你
一些很好的好处,比如内置在
管理中,内置能力运行
即服务等等。

On the other hand, app servers give you some nice benefits like built-in management, built-in ability to run as a service, etc.

您甚至可以使用混合
方法:使用嵌入式服务器在本地开发
,然后在生产中部署到
容器中。但是,这个
a有点奇怪:如果你经历了制造一个正确的WAR文件的
麻烦,
IDE应该真的能够将
部署到一个容器
中。

You could even use a hybrid approach: use an embedded server to develop locally, then deploy into a container in production. But that's a bit weird: if you go through the trouble of making a proper WAR file, IDEs should really be able to handle deployment into a container adequately.

顺便说一下,你发现热重新部署
是很奇怪的;除非
你遇到一些奇怪的
角落案件,否则Tomcat不应该有
的问题...

BTW, it's weird that you have issues with hot-redeploy; Tomcat shouldn't be having issues with it unless you're running into some strange corner case...

我应该使用Servlet API吗?

Should I be using Servlet API?

这是#1的正交。你
可以很好地嵌入Jetty和
实现Servlets。您还可以
通过ServerServlet
在Tomcat
中使用Restlet API http:/ /www.restlet.org/documentation/1.0/faq#02

我个人觉得Servlet API到
非常直截了当你得到
很好的东西,比如并发和
状态管理。我不太清楚
这意味着RESTful设计
不受支持,但如果Restlets
更好地满足您的要求,那么
然后使用它......

I personally find the Servlet API to be pretty straight-forward.You get nice things like concurrency and state management. I don't quite know what that means that RESTful design is not supported, but if Restlets address your requirements better, then use that...