做爲Java工程師,服務器對於咱們的開發工做很重要,將咱們開發好的項目放到服務器上用戶纔可使用。咱們經常使用到的服務器有Tomcat、Apache、Nginx等等。對於咱們所開發的應用,如何選擇合適的服務器是個問題。下面簡單的介紹Web服務器、Servlet容器、應用服務器的基本概念以及各類服務器的應用場景。
本文是Google上查詢的幾遍文章的部分截斷,我本身加上了中文翻譯。html
Apache、IIS、Nginxjava
Web Servers are responsible for serving static content
e.g. HTML over HTTP protocol.
Web服務器的主要職責是服務靜態頁面
,例如,以HTTP協議傳輸的HTML頁面web
In a simple situation,a user type in a URL in browser(a client),and get a web page to read.So what the server does is sending a web page to the client.The transformation is in HTTP protocol which specifies the format of request and response message.
一個簡單的應用場景,用戶在瀏覽器(客戶端)輸入URL而且開始瀏覽網頁。那麼服務器所作的就是發送一個靜態頁面到客戶端,傳輸的信息是經過指定了請求和響應格式的HTTP協議。
數據庫
Tomcat、Jetty瀏覽器
A servlet-container supports only the servlet API(including JSP,JSTL)
Servlet容器僅僅支持Servlet API,包括JSP、JSTL安全
Servlet container are only responsible for generating HTML by executing JSP and Servlet
on Server side.So servlet container is essentially a part of a web server that interacts with the servlets.
Servlet容器惟一的職責就是經過執行服務器上的JSP和Servlet來產生HTML頁面。那麼Servlet容器本質上是Web服務器的一部分,只不過加上了和Sevlet交互的接口。
服務器
Servlet is an interface defined in javax.servlet package.It declares three essential methods for the life cycle of a servlet – init(),service(),and destroy().They are implemented by every servlet and invoked at specific times by the server.
Servlet是定義在javax.servlet包下面的接口。對於一個Servlet的聲明週期,它聲明瞭三種內在的方法——init(),servie()和destroy().在服務器上每個Servlet被喚醒的時候,他們都會執行。app
How Servlet Container and web server process a request ?ide
invoked once when the servlet is loaded first time
)
WebSphere,WebLogic,JBossui
Application Server is responsible for generating dynamic content by executing server side code e.g. JSP,Servlet or EJB.
應用服務的主要職責就是經過執行服務器代碼產生動態頁面,列如,JSP,Servlet或者EJB。
Application server is responsible for serving dynamic content
,managing EJB pool,facilitating distributed transation
,facilitating application lookup over JNDI,application security and others.
應用服務器的主要任務是服務動態內容,管理EJB Pool,促進事務分配,促進應用查找JNDI,應用安全等等。
An application server supports the whole JavaEE - EJB,JMS,CDI,JTA
應用服務器支持整個JavaEE體系
An application usually includes a Web server
,but also adds a lot of more features.The most important is that it manages objects.Whether they will be Servlets,EJBs,JMS listenters.
一個應用服務器一般包括一個Web服務器,可是額外加了不少特性。最重要的就是它管理對象(Servlets,EJB,JMS)
Tomcat is not exactly an application server, it's more of a servlet engine or web container or also known as servlet containers because it provides the runtime environment for Servlet and JSP but doesn't provide the services like EJB and distributed transaction which are a key feature of the application server in Java JEE world
Tomcat不徹底是一個應用服務器,它只是一個Servlet引擎或者是一個Web容器或者是咱們衆所周知的Servlet容器,由於它只提供了Servlet和JSP的運行環境,可是沒有提供像是EJB和事務控制的部分,而這部分纔是JEE領域裏面應用服務器的特色。
It’s expected from a Web server to provide HTTP protocol level service while application server is supposed to provide more powerful and dynamic Web service and business level service via EJB(Enterprise Java Beans).Essential services like Database connection pooling is not only provided by application server but also by Servlet containers like Tomcat.
能夠預計的是一個Web服務器提供HTTP協議層次的服務,應用服務器應該是提供更多的強大的動態的Web服務,而且包括商務級別的服務經由EJB。像數據庫鏈接池這樣必要的服務不單單是應用服務器支持,像是Tomcat這樣的Servlet容器也支持。
You need a Web server like Apache HTTPD if you are serving static web pages.If you have a Java application with just JSP and Servlet to generate dynamic content then you need Servlet containers like Tomcat or Jetty while,if you have JavaEE application using EJB,distributed transation,messaging and other fancy features than you need a full fledged application server like JBoss,WebSphere or WebLogic.
若是你爲靜態頁面提供服務,你就須要像Apache HTTPD這樣的Web服務器。若是你有一個僅僅是JSP和Servlet的JAVA應用去生成動態的頁面內容,你須要一個像Tomcat或者Jetty這樣的Servlet容器。若是你的JAVAEE應用使用了EJB,事務管理,消息和其餘一些複雜華麗的特色,你就須要一個徹底成熟的應用服務器,列如JBoss,WebSphere或者WebLogic.
For example,one of the popular setups is Apache fronting Tomcat
or Ngnix fronting Tomcat
好比,一個很是流行的作法就是在Tomcat上面加一Apache或者是在Tomcat上加Nginx服務。
What is the difference between application server and web server ?
Web Server is designed to serve HTTP Content. Application Server can also serve HTTP Content but is not limited to just HTTP. It can be provided other protocol support such as RMI/RPC
Web服務器旨在爲HTTP內容提供服務。應用服務器也能夠爲HTTP內容提供服務,可是它不單單限於HTTP,它能夠提供其餘的協議支持,例如RMI/RPC。
Web Server is mostly designed to serve static content, though most Web Servers have plugins to support scripting languages like Perl, PHP, ASP, JSP etc. through which these servers can generate dynamic HTTP content.
儘管大多數的Web服務器有支持腳本語言的插件,經過這些腳本語言,好比PHP,ASP,JSP,能夠產生動態的HTTP內容,可是Web服務器大多數旨在爲靜態頁面服務。
Most of the application servers have Web Server as integral part of them, that means App Server can do whatever Web Server is capable of. Additionally Application Server have components and features to support Application level services such as Connection Pooling, Object Pooling, Transaction Support, Messaging services etc.
大多數的應用服務器都包括Web服務器來構成他們完整的體系,這也就意味着應用服務器能夠作任何Web服務器有能力作的事情。除此以外,應用服務器包含了一些可以達到應用層次的組件和特色,例如鏈接池,對象池,事務支持,消息服務等。
As web servers are well suited for static content and app servers for dynamic content, most of the production environments have web server acting as reverse proxy to app server. That means while service a page request, static contents such as images/Static html is served by web server that interprets the request. Using some kind of filtering technique (mostly extension of requested resource) web server identifies dynamic content request and transparently forwards to app serve 對於靜態頁面來講Web服務器很適,而應用服務器適合動態內容,可是大多數的生產環境都是有一個Web服務器做爲應用服務器的反向代理。這就意味着一個頁面的請求,像圖片和靜態HTML這樣的靜態內容是由Web服務器提供服務。使用一些像過濾器這樣的技術,Web服務器鑑別動態內容請求並顯示的導向應用服務器。