Web應用
在Java EE平臺中,**Web組件**為Web服務器提供動態擴充的能力。Web組件可以是Java Servlet,JavaServer Faces技術實現的Web頁面,Web服務端點,或者JSP頁面。圖3-1說明了使用Servlet的Web服務器和Web應用之間的交互。客戶端發送一個HTTP請求給Web服務器。一個實現了Java Servlet和JavaServer Page(譯注:簡稱JSP,JSP相當于服務器解析JSP語言動態生成網頁的技術)技術的Web服務器把請求轉換成一個HTTPServletRequest對象。這個對象被傳送到一個Web組件中,Web組件能夠與JavaBeans組件或者一個數據庫交換,來生成動態內容。這個Web組件然后就能生成一個HTTPServletResponse或者把請求傳遞到另一個Web組件。一個Web組件最終生成一個HTTPServletResponse對象。Web服務器把這個對象轉換成一個HTTP響應然后把它返回給客戶端。
圖3-1 Java Web服務器請求處理
Servlet是Java編程語言類,動態地處理請求以及構建響應。諸如JavaServer Faces和Facelets的Java技術,被用來構建交互式Web應用程序(框架也可以用作此目的)。盡管Servlet、Java Server Faces和Facelets頁面可以用來完成類似的事情,但是他們各有優點。Servlets最適于面向服務的應用程序(Web服務端點可以實現為Servlet)以及面向展示的應用程序的控制部分,例如分發請求和處理非文本數據。Java Server Faces 和 Facelet 頁面更適合于生成基于文本的標記語言,例如XHTML,并且更廣泛地使用與面向展示的應用程序。
Web組件由被稱為Web容器的運行時平臺支持。一個Web容器以分配、安全、并發和生命周期管理的形式提供這些服務。一個Web容器也為Web組件提供了如命名、transation和email等API訪問。
Web應用程序的某些方面行為可以在應用程序安裝或者部署到Web容器的時候配置。配置信息可以用Java EE 注解來指定,或者被維護在一個被稱為Web應用程序部署描述(Deployment Descriptor,DD)的XML格式的文本文件。一個Web應用程序部署描述必須遵守Java Servlet Specification中描述的模式。
這一節給出了開發Web應用程序包含的活動的簡短綜述。首先,它總結了Web應用程序生命周期,解釋了怎么打包和在GlassFish服務器部署非常簡單的Web應用程序。該節會繼續講解配置Web應用程序,探討如何制定最常用的配置參數。
Web Applications
In the Java EE platform,?**web components**?provide the dynamic extension capabilities for a web server. Web components can be Java servlets, web pages implemented with JavaServer Faces technology, web service endpoints, or JSP pages.?[Figure?3-1](http://docs.oracle.com/javaee/6/tutorial/doc/geysj.html#bnads)illustrates the interaction between a web client and a web application that uses a servlet. The client sends an HTTP request to the web server. A web server that implements Java Servlet and JavaServer Pages technology converts the request into an?HTTPServletRequest
object. This object is delivered to a web component, which can interact with JavaBeans components or a database to generate dynamic content. The web component can then generate an?HTTPServletResponse
or can pass the request to another web component. A web component eventually generates a?HTTPServletResponse
object. The web server converts this object to an HTTP response and returns it to the client.
Figure?3-1 Java Web Application Request Handling
Servlets are Java programming language classes that dynamically process requests and construct responses. Java technologies, such as JavaServer Faces and Facelets, are used for building interactive web applications. (Frameworks can also be used for this purpose.) Although servlets and Java Server Faces and Facelets pages can be used to accomplish similar things, each has its own strengths. Servlets are best suited for service-oriented applications (web service endpoints can be implemented as servlets) and the control functions of a presentation-oriented application, such as dispatching requests and handling nontextual data. Java Server Faces and Facelets pages are more appropriate for generating text-based markup, such as XHTML, and are generally used for presentation–oriented applications.
Web components are supported by the services of a runtime platform called a?**web container**. A web container provides such services as request dispatching, security, concurrency, and lifecycle management. A web container also gives web components access to such APIs as naming, transactions, and email.
Certain aspects of web application behavior can be configured when the application is installed, or?**deployed**, to the web container. The configuration information can be specified using Java EE annotations or can be maintained in a text file in XML format called a web application deployment descriptor (DD). A web application DD must conform to the schema described in the Java Servlet specification.
This chapter gives a brief overview of the activities involved in developing web applications. First, it summarizes the web application lifecycle and explains how to package and deploy very simple web applications on the GlassFish Server. The chapter moves on to configuring web applications and discusses how to specify the most commonly used configuration parameters.