Part II 3.从Web应用开始(Getting Started with Web Applications)

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服务器请求处理

Part II 3.从Web应用开始(Getting Started with Web Applications)_第1张图片

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

Part II 3.从Web应用开始(Getting Started with Web Applications)_第2张图片

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.

你可能感兴趣的:(Part II 3.从Web应用开始(Getting Started with Web Applications))