毕业设计英文翻译JSP

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JSP Overview
JSP is the latest Java technology for web application development and is based on the servlet technology introduced in the previous chapter. While servlets are great in many ways, they are generally reserved for programmers. In this chapter, we look at the problems that JSP technology solves, the anatomy of a JSP page, the relationship between servlets and JSP, and how the server processes a JSP page.
In any web application, a program on the server processes requests and generates responses. In a simple one-page application, such as an online bulletin board, you don't need to be overly concerned about the design of this piece of code; all logic can be lumped together in a single program. However, when the application grows into something bigger (spanning multiple pages, using external resources such as databases, with more options and support for more types of clients), it's a different story. The way your site is designed is critical to how well it can be adapted to new requirements and continue to evolve. The good news is that JSP technology can be used as an important part in all kinds of web applications, from the simplest to the most complex.
Therefore, this chapter also introduces the primary concepts in the design model recommended for web applications and the different roles played by JSP and other Java technologies in this model.
The Problem with Servlets
In many Java servlet-based applications, processing the request and generating the response are both handled by a single servlet class. shows how a servlet class often looks.
Example 3-1. A typical servlet class
public class OrderServlet extends HttpServlet {
public void doGet((HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
("text/html");
PrintWriter out = ( );
if (isOrderInfoValid(request)) {
saveOrderInfo(request);
("<html>");
(" <head>");
(" <title>Order Confirmation</title>");
(" </head>");
(" <body>");
(" <h1>Order Confirmation</h1>");
renderOrderInfo(request);
(" </body>");
("</html>");
}
...
If you're not a programmer, don't worry about all the details in this code. The point is that the servlet contains request processing and business logic (implemented by methods such as isOrderInfoValid() and saveOrderInfo( )), and also generates the response HTML code, embedded directly in the servlet code using println( ) calls. A more structured servlet application isolates different pieces of the processing in various reusable utility classes and may also use a separate class library for generating the actual HTML elements in the response. Even so, the pure servlet-based approach still has a few problems:
Thorough Java programming knowledge is needed to develop and maintain all aspects of the application, since the processing code and the HTML elements are lumped together.
Changing the look and feel of the application, or adding support for a new type of client (such as a WML client), requires the servlet code to be updated and recompiled.
It's hard to take advantage of web page development tools when designing the application interface. If such tools are used to develop the web page layout, the generated HTML must then be manually embedded into the servlet code, a process which is time consuming, error prone, and extremely boring.
Adding JSP to the puzzle lets you solve these problems by separating the request processing and business logic code from the presentation, as illustrated in . Instead of embedding HTML in the code, place all static HTML in a JSP page, just as in a regular web page, and add a few JSP elements to generate the dynamic parts of the page. The request processing can remain the domain of the servlet, and the business logic can be handled by JavaBeans and EJB components.
Figure 3-1. Separation of request processing, business logic, and presentation
As I mentioned before, separating the request processing and business logic frompresentation makes it possible to divide the development tasks among people with different skills. Java programmers implement the request processing and business logic pieces, web page authors implement the user interface, and both groups can use best-of-breed development tools for the task at hand. The result is a much more productive development process. It also makes it possible to change different aspects of the application independently, such as changing the business rules without touching the user interface.
This model has clear benefits even for a web page author without programming skills, working alone. A page author can develop web applications with many dynamic features, using
the JSP standard actions and the JSTL libraries, as well as Java components provided by open source projects and commercial companies.
JSP Processing
Just as a web server needs a servlet container to provide an interface to servlets, the server needs a JSP container to process JSP pages. The JSP container is responsible for intercepting requests for JSP pages. To process all JSP elements in the page, the container first turns the JSP page into a servlet (known as the JSP page implementation class). The conversion is pretty straightforward; all template text is converted to println( ) statements similar to the ones in the handcoded servlet shown in , and all JSP elements are converted to Java code that implements the corresponding dynamic behavior. The container then compiles the servlet class.
Converting the JSP page to a servlet and compiling the servlet form the translation phase. The JSP container initiates the translation phase for a page automatically when it receives the first request for the page. Since the translation phase takes a bit of time, the first user to request a JSP page notices a slight delay. The translation phase can also be initiated explicitly; this is referred to as precompilation of a JSP page. Precompiling a JSP page is a way to avoid hitting the first user with this delay. It is discussed in more detail in .
The JSP container is also responsible for invoking the JSP page implementation class (the generated servlet) to process each request and generate the response. This is called the request processing phase. The two phases are illustrated in .
Figure 3-2. JSP page translation and processing phases
As long as the JSP page remains unchanged, any subsequent request goes straight to the request processing phase ., the container simply executes the class file). When the JSP page is modified, it goes through the translation phase again before entering the request processing phase.
The JSP container is often implemented as a servlet configured to handle all requests for JSP pages. In fact, these two containers—a servlet container and a JSP container—are often combined in one package under the name web container.
So in a way, a JSP page is really just another way to write a servlet without having to be a Java programming wiz. Except for the translation phase, a JSP page is handled exactly like a regular servlet; it's loaded once and called repeatedly, until the server is shut down. By virtue of being an automatically generated servlet, a JSP page inherits all the advantages of a servlet described in : platform and vendor independence, integration, efficiency, scalability, robustness, and security.
JSP Elements
There are three types of JSP elements you can use: directive, action, and scripting. A new construct added in JSP is an Expression Language (EL) expression; let's call this a forth element type, even though it's a bit different than the other three.
Directive elements
The directive elements, shown in , specify information about the page itself that remains the same between requests—for example, if session tracking is required or not, buffering requirements,
Table 3-1. Directive elements
components
JSP elements, such as action and scripting elements, are often used to work with JavaBeans components. Put succinctly, a JavaBeans component is a Java class that complies with certain coding conventions. JavaBeans components are typically used as containers for information that describes application entities, such as a customer or an order.
JSP Application Design with MVC
JSP technology can play a part in everything from the simplest web application, such as an online phone list or an employee vacation planner, to complex enterprise applications, such as a human resource application or a sophisticated online shopping site. How large a part JSP plays MVC was first described by Xerox in a number of papers published in the late 1980s. The key differs in each case, of course. In this section, I introduce a design model called Model-View-Controller (MVC), suitable for logic into three distinct units: the Model, the View, and the Controller. In a server application, we commonly classify the parts of the application as business logic, presentation, and request processing. Business logic is the term used for the manipulation of an application's data, such as customer, product, and order information. Presentation refers to how the application data is displayed to the user, for example, position, font, and size. And finally, request processing is what ties the business logic and presentation parts together. In MVC terms, the Model corresponds to business logic and data, the View to the presentation, and the Controller to the request processing.
Why use this design with JSP? The answer lies primarily in the first two elements. Remember that an application data structure and logic (the Model) is typically the most stable part of an application, while the presentation of that data (the View) changes fairly often. Just look at all the face-lifts many web sites go through to keep up with the latest fashion in web design. Yet, the data they present remains the same. Another common example of why presentation should be separated from the business logic is that you may want to present the data in different languages or present different subsets of the data to internal and external users. Access to the data through new types of devices, such as cell phones and personal digital assistants (PDAs), is the latest trend. Each client type requires its own presentation format. It should come as no surprise, then, that separating business logic from the presentation makes it easier to evolve an application as the requirements change; new presentation interfaces can be developed without touching the business logic.
This MVC model is used for most of the examples in this book. In Part II, JSP pages are used as both the Controller and the View, and JavaBeans components are used as the Model. The examples in Chapter 5 through Chapter 9 use a single JSP page that handles everything, while Chapter 10 through Chapter 14 show how you can use separate pages for the Controller and the View to make the application easier to maintain. Many types of real-world applications can be developed this way, but what's more important is that this approach allows you to examine all the JSP features without getting distracted by other technologies. In Part III, we look at other possible role assignments when JSP is combined with servlets and Enterprise JavaBeans.
JSP 概览
JSP是一种用于Web应用程序开发的最新的Java技术,它是成立在servlet 技术基础之上的。

尽管servlet 的功能在很多方面来讲都很壮大,但它通常属于程序员专有的领域。

在本章中,咱们将介绍JSP 技术能够解决哪些问题,对JSP 页面的剖析,servlet 和JSP 之间的关系,和效劳器处置JSP 页面的方式等。

在任何Web 应用程序中,都有一个效劳器上的程序来处置请求并产生应答。

关于简单的单页式应用程序,例如一个在线公告牌,你并非需要过量地考虑代码的设计,而且所有的逻辑都能够堆在一个程序中。

但当应用程序变得愈来愈大时(如跨越了多个页面,利用数据库之类的外部资源,并为更多类型的客户提供了更多的选项和支持),情形就完全不一样了。

站点的设计方式直接关系到它适应新需求的能力和继续改良的能力。

令人快乐的是,JSP 技术能够作为所有类型Web 应用程序的一个重要部份,从最简单的到最复杂的应用程序都是如此。

因此,本章还将介绍推荐利用的Web应用程序设计模型的大体概念,和JSP和其他Java 技术在那个模型中所扮演的不同角色。

servlet 所带来的问题
在许多基于Java servlet 的应用程序中,处置请求和产生应答是由同一个servlet 类来执行的。

例3-1 显示了servlet 通常的外观:
例3-1:典型的servlet 类
public class OrderServlet extends HttpServlet {
public void doGet((HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
("text/html");
PrintWriter out = ();
if (isOrderInfoValid(request)) {
saveOrderInfo(request);
("<html>");
(" <head>");
(" <title>Order Confirmation</title>");
(" </head>");
(" <body>");
(" <h1>Order Confirmation</h1>");
renderOrderInfo(request);
(" </body>");
("</html>");
}
...
若是你不是程序员,也不用为这些代码中的细节担忧。

关键的一点是,servlet 包括了请求的处置和商务逻辑(通过像isOrderInfoValid()和saveOrderInfo()如此的方式来实现),而且产生了应答的HTML代码,这些代码是通过挪用println()函数而直接嵌入在servlet 代码中的。

一个加倍结构化的servlet应用程序能够将处置进程分成各个不同的部份,封装在各类可重用的工具类中,也可能利用一个单独的类库来产生应答中的实际的HTML元素。

尽管如此,纯servlet 的方案仍是存在以下一些问题:
●开发和保护应用程序的所有部份需要有深厚的Java编程知识,因为处置代码和HTML 元素是交织在一路的。

●改变应用程序的外观和风格,或加入对某种新类型客户机(如WML客户机)的支持时,都需要更新并从头编译servlet 代码。

●很难利用网页开发工具的优势来设计应用程序界面。

若是利用这些工具来开发网页布局的话,生成的HTML 代码必需被手工嵌入到servlet 代码中,那个进程既耗时又容易犯错,而且极度枯燥乏味。

引入JSP 能够将请求处置和商务逻辑与外观呈现分离开来,如图3-1 所示。

现在并非是将HTML嵌入到代码中,而是将所有静态HTML放到JSP 页面中,就像一个通常的网页一样,然后加入一些JSP 元素来产生页面的动态部份。

对请求的处置工作能够留给servlet 程序员来做,商务逻辑那么能够由JavaBeans 和EJB 组件来处置。

图3-1:将请求处置、商务逻辑和外观呈现分离
正如前面提到的那样,将请求处置和商务逻辑与外观呈现分开后,许诺咱们将开发任务
分派给拥有不同技术的人们。

Java 程序员负责实现请求处置和商务逻辑部份,网页设计师那么实现用户界面,而且这两组人员都能够利用手边最好用的工具软件来完成任务。

其结果确实是咱们取得了更为多产的开发进程。

这也使得咱们能够单独改变应用程序的某个部份,例如修改商务规那么,而没必要触及用户界面。

即便关于一名独立工作且没有编程技术的网页设计师来讲,那个模型也有着明显的优势。

网页设计师能够利用JSP标准行为和JSTL库和开源组织或商业公司提供的Java 组件,来开发拥有许多动态特性的Web 应用程序。

JSP 处置进程
正如Web 效劳器需要一个servlet 容器来提供对servlet 的接口一样,效劳器一样也需要一个JSP 容器来处置JSP 页面。

JSP 容器负责说明对JSP 页面的请求。

为了处置页面中所有的JSP 元素,容器第一要把JSP 页面转化成servlet[称为“JSP 页面实现类(JSP Page implementation class)”]。

这种转换是超级直接的,所有的模板文本都被转换成与例3-1 中所示的手工编码的servlet相类似的println()语句,而且所有的JSP 元素都被转换成实现相应动态行为的Java 代码。

然后容器再编译servlet类。

将JSP 页面转换为servlet 并编译该servlet,这确实是翻译时期(translation phase)。

当第一次收到对某个页面的请求时,JSP容器会自动开始对那个页面的翻译时期。

固然,翻译时期需要一点时刻来完成,因此用户会注意到当某个JSP 页面被第一次请求时,会有一点延迟。

翻译时期也能够被显式地启动,这称为JSP 页面的预编译(precompilation)。

对JSP 页面进行预编译能够幸免延迟问题。

在第十六章中将详细介绍预编译。

JSP容器还负责挪用JSP页面实现类(生成的servlet),以处置每一个请求并生成应答。

那个时期被称为“请求处置时期(request processing phase)”。

这两个时期如图3-3 所示。

图3-2:JSP 页面翻译时期和请求处置时期
只要JSP 页面维持不变,任何后续的请求都会直接进入请求处置时期(也确实是说,容器仅执行类文件)。

当JSP 页面被修改后,后续请求将再次通过翻译时期,然后才进入请求处置时期。

JSP 容器一般是作为一种servlet 来实现的,并被配置为处置所有对页面的请求。

事实
上,servlet 容器和JSP 容器这两种容器通常都组合在一个名为web container 的软件包中。

因此,通过这种方式,JSP 页面事实上是编写servlet 的另外一种方式,而你却没必若是一名Java 编程高手。

而且,除翻译时期,JSP 页面的处置进程与常见的servlet完全相同:载入一次后多次重复执行,直到效劳器关闭为止。

作为一种自动生成的servlet,JSP 页面继承了第二章中所讲的servlet 的所有优势:平台和厂商独立性,集成化,高效性,可缩放性,健壮性和平安性。

JSP 元素
JSP 有三种类型的元素:指令元素(directive element)、行为元素(action element)和脚本元素(scripting elment)。

指令元素
表3-1 所示的指令元素用于指定关于页面本身的一些信息,这些信息在各个页面请求间维持不变。

这些信息内容包括:是不是需要会话跟踪,对缓冲的要求,和用于报告错误的页面名称,等等。

表3-1:指令元素
JavaBeans 组件
JSP 元素,例如行为元素和脚本元素,常经常使用来与JavaBeans 一路利用。

简单地说,一个JavaBeans 组件确实是一个用特定的编码约定编译的Java 类。

JavaBeans 组件通经常使用作容纳描述应用程序中某类实体(例如客户或定单)的信息的容器。

利用MVC 设计JSP 应用程序
JSP 技术能够应用于各类事务,从最简单的Web 应用程序,例如一个在线簿或员工休假安排,到功能齐全的企业级应用程序,例如人力资源应用程序或复杂的在线购物站点。

固然,在各类应用中,JSP 占的比例大小不一。

在这一节中,咱们将介绍一种同时适用于简单应用程序和复杂应用程序的设计模型,称为MVC(MODEL-View-Controller,模型-视图-操纵器)。

MVC 第一是由Xerox(施乐)公司在80 年代后期发表的一系列论文中提出的。

利用MVC 的关键点是将逻辑分成三个各自独立的单位:模型、视图和操纵器。

在一个效劳器应用程序中,咱们通常将应用程序分成以下三部份:商务逻辑、外观呈现和请求处置。

术语“商务逻辑(business logic)”指的是对应用程序的数据进行处置的规那么,例如客户、产品、定单信息等数据。

“外观呈现(presentation)”指的是应用程序数据是如何展现在用户眼前的,例如位置、字体和尺寸等。

最后,“请求处置(request processing)”是指将商务逻辑和外观呈现联系起来的部份。

若是用MVC的术语来讲,模型对应着商务逻辑和数据,视图对应外观呈现,操纵器那么对应请求处置。

什么缘故要对JSP 利用这种设计模型?答案要紧在于前两个元素。

应用程序的数据结构和逻辑(也确实是模型)一般是一个应用程序中最稳固的部份,可是这些数据的表现形式(也确实是视图)却是常常改变的。

只要看看那些站点为了跟上最新的网页设计时尚而不断更新门面的进程就明白了。

但是,它们所表现的数据却是维持不变的。

另一个说明什么缘故要把商务逻辑和外观呈现分开的常见例如是,你可能需要用不同的
语言来表现数据,或向内部和外部用户提供不同的数据子集。

通过新型的设备来访问数据,例如利用移动和PDA(Personal Digital Assistant,个人数字助理)是最新的趋势。

每种类型的客户都要求有自己的表现格式。

因此,顺理成章,将商务逻辑和外观呈现分开能够更易地依照需求的改变来改良应用程序,并在不触动商务逻辑的情形下开发新的外观界面。

本书中的绝大部份例如都利用了MVC模型。

在第二部份中,JSP页面既被用作操纵器又被用作视图,JavaBeans 组件被用作模型。

第五章至第九章的例如利用一个单独的JSP 页面来处置一切情形,而第十章至第十三章将指导读者如何为操纵器和视图利用单独的页面,从而使得整个应用程序更易于保护。

现实世界中许多类型的应用程序都能够利用这种方式来进行开发,但更重要的是,这种方式使得咱们能够利用JSP 的所有特性,而不用涉及其他的技术。

在第三部份中,咱们将看到当JSP 与servlet 和EJB 组合利历时,其他可能的角色分派方案。

相关文档
最新文档