Java服务器
作为Java工程师,服务器对于我们的开发工作很重要,将我们开发好的项目放到服务器上用户才可以使用。我们常用到的服务器有Tomcat、Apache、Nginx等等。对于我们所开发的应用,如何选择合适的服务器是个问题。下面简单的介绍Web服务器、Servlet容器、应用服务器的基本概念以及各种服务器的应用场景。
本文是Google上查询的几遍文章的部分截断,我自己加上了中文翻译。
WEB Server
Example
Apache、IIS、Nginx
Features and Responsibility
Web Servers are responsible for serving static content
e.g. HTML over HTTP protocol.
Web服务器的主要职责是服务静态页面
,例如,以HTTP协议传输的HTML页面
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协议。
Servlet Container
Example
Tomcat、Jetty
Features and Responsibility
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
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被唤醒的时候,他们都会执行。
How Servlet Container and web server process a request ?
- Web server receives HTTP request
- Web server forwards the request to servlet containter
- The servlet is dynamically retrieved and loaded into the address space of the container,if it is not in the container.
- The container invokes the init() method of the servlet for initialization(
invoked once when the servlet is loaded first time
) - The container invokes the service() method of the servlet to process the HTTP request,read data in the request and formulate a response.The servlet remains in the container’s address space and can process other HTTP requests.
Application Server
Example
WebSphere,WebLogic,JBoss
Features and Responsibility
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
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领域里面应用服务器的特点。
Summary
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服务器鉴别动态内容请求并显示的导向应用服务器。