登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 管理学类 > 信息管理与信息系统 > 正文

Servlet和JSP技术简述外文翻译资料

 2022-11-27 02:11  

An Overview of Servlet and JSP Technology

1.1 A Servletrsquo;s Job

Servlets are Java programs that run on Web or application servers, acting as a middle layer between request coming from Web browsers or other HTTP clients and databases or applications on the HTTP server. Their job is to perform the following tasks, as illustrated in Figure 1-1.

Figure 1-1

1.Read the explicit data sent by the client.

The end user normally enters this data in an HTML from on a Web page. However, the data could also come from an applet or a custom HTTP client program.

2.Read the implicit HTTP request data sent by the browser.

Figure1-1 shows a single arrow going from the client to the Web server(the layer where servlets and JSP execute), but there are really two varieties of data: the explicit data that the end user enters in a form and the behind-the-scenes HTTP information. Both varieties are critical. The HTTP information includes cookies, information about media types and compression schemes the browser understands, and so on.

3.Generate the results.

This process may require talking to a database, executing an RMI or EJB call, invoking a Web service, or computing the response directly. Your real data may be in a relational database. Fine. But your database probably doesnrsquo;t speak HTTP or return results in HTML, so the Web browser canrsquo;t talk directly to the database. Even if it could, for security reasons, you probably would not want it to. The same argument applies to most other applications. You need the Web middle layer to extract result inside a document.

4.Send the explicit data (i.e., the document) to the client.

This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), or even a compressed format like gzip that is layered on top of some other underlying format. But, HTML is by far the most common, so an important servlet/JSP task is to wrap the results inside of HTML.

5.Send the implicit HTTP response data.

Figure1-1 shows a single arrow going from the Web middle layer (the servlet or JSP page) to the client. But, there are really two varieties of data sent: the document itself and the behind-the-scenes HTTP information. Again, both varieties are critical to effective development.Sending HTTP response data involves telling the browser or other client what type of document is being returned (e.g.,HTML), setting cookies and caching parameters, and other such tasks.

1.2 Why Build Web Pages Dynamically?

Many client requests can be satisfied by prebuilt documents, and the server would handle these requests without invoking servlets. In many cases, however, a static result is not sufficient, and a page needs to be generated for each request. There are a number of reasons why Web pages need to be built on-the-fly:

1.The Web page is based on data sent by the client.

For instance, the results page from search engines and other-confirmation pages at on-line stores are specific to particular user requests.You donrsquo;t know what to display until you read the data that the user submits. Just remember that the user submits two kinds of data: explicit (i.e., HTML from data) and the implicit( i.e., HTTP request headers). Either kind of input can be used to build the output page. In particular, it is quite common to build a user-specific page based on a cookie value.

2.The web page is derived from data that changes frequently.

If the page changes for every request, then you certainly need to build the response at request time. If it changes only periodically, however, you could do it two ways: you could periodically build a new Web pages on the server (independently of client requests), or you could wait and only build the page when the user requests it. The right approach depends on the situation, but sometimes it is more convenient to do the latter: wait for the user request. For example, a weather report or news headlines site might build the pages dynamically, perhaps returning a previously built page if that page is still up to date.

3.The Web page uses information from corporate databases or other server-side sources.

If the information is in the database, you need server-side processing even if the client is using dynamic Web content such as an applet. Imagine using an applet by itself for a search engine site:

“Downloading 50 terabyte applet, please wait!” Obviously, that is silly; you need to talk to the database. Going from the client to the Web tier to the database(a three-tier approach) instead of from an applet directly to the database (a two-tier approach) provides increased flexibility and security with little or no performance penalty. After all, the database call is usually the rate-limiting step, so going through the Web server does not slow things down. In fact, a three-tier approach is often faster because the middle tier can perform caching and connection pooling.

In principle, servlets are not restricted to Web or application servers that handle HTTP requests but can be used for other types of servers as well. For example, servlets could be embebed in FTP or mail servers to extend their functionality. And, a servlet API for SIP (Session Initiation Protocol) servers was recently standardized (see http://jcp.org/en/jsr/detail?id=116). In practice, however, this use of servlets has not caught on, and wersquo;ll only be discussing HTTP servlets.

1.3The Adventages of Servlets Over “Traditional” CGI

Java servlets are more efficient, easier to use,more powerful, more portable, safer and cheaper than traditional CGI and many alternative CGI-like technologies.

1.Efficient

With traditional CGI, a new process is started for each HTTP request. If the CGI program itself is relatively short, the overhead of starting the process can dominate the execution time. With servlets, the Java virtu

剩余内容已隐藏,支付完成后下载完整资料


Servlet和JSP技术简述

1.1 Servlet的功能

Servlets是运行在Web或应用服务器上的Java程序,它是一个中间层,负责连接来自Web浏览器或其他的HTTP客户端和HTTP服务器上的数据库或应用程序。Servlet的工作是执行下面的任务,如图1-1所示。

图1-1

1.读取客户端发送的显示数据。

客户端用户一般在页面的HTML表单中输入这些数据。然而,数据还有可能来自applet或定制的HTTP客户程序。

2.读取由浏览器发送的隐式请求数据。

图1-1中显示了一条从客户端到Web服务器的单箭头,但实际上从客户端传送到Web服务器的数据有两种,它们分别为用户在表单中输入的显示数据,以及后台的HTTP信息。两种数据都很重要。HTTP信息包括cookie、浏览器所能识别的媒体类型和压缩模式等。

3.生成结果。

这个过程可能需要访问数据库、执行RMI或EJB调用、调用Web 服务,或者直接计算得出对应的响应。实际的数据可能存储在关系型数据库中。但是该数据可能不理解HTTP,或者不能返回HTML形式的结果,所以Web浏览器不能直接与数据库进行会话。即使它能够做到这一点,为了安全上的考虑,我们也不希望让它这么做。对应大多数其他应用程序,也存在类似的问题。因此,我们需要Web中间层从HTTP流中提取输入数据,与应用程序会话,并将结果嵌入到文档中。

4.向客户发送显示数据(即文档)

这个文档可以用各种格式发送,包括文本(HTML或XML),二进制(GIF图像),甚至是可以建立在其他底层格式之上的压缩格式,如gzip。但是,到目前为止,HTML式最常用的格式,故而Servlet/JSP的重要任务之一就是将结果包装到HTML中。

5.发送隐式的HTTP相应数据。

图1-1中显示了一条从Web中间层到客户端的单箭头。但是,实际发送的数据有两种:文档本身,以及后台的HTTP信息。同样,两种数据对开发来说都是至关重要的。HTTP响应数据的发送过程涉及告知浏览器或其他客户程序所返回文档的类型(如HTML),设置cookie和缓存参数,以及其他类似的任务。

1.2动态构建网页的原因

预先建立的文档可以满足客户的许多请求,服务器无需调用servlet就可以处理这些请求。然而,许多情况下静态的结果不能满足要求,我们需要针对每个请求生成一个页面。实时构建页面的理由有两种:

1.网页基于客户发送的数据。

例如,搜索引擎生成的页面,以及在线订单确认页面,都要针对特定的用户请求而产生。在没有读取到用户提交的数据之前,我们不知道应该显示什么。要记住,用户提交两种类型的数据:显示(即HTML表单的数据)和隐式(即HTML请求的报头)。两种输入都可用来构建输出页面。基于cookie值针对具体用户构建页面的情况尤其普遍。

2.页面由频繁改变的数据导出。

如果页面需要根据每个具体的请求作出相应的改变,当然需要在请求发生时构建响应。但是如果页面周期性的改变,我们可以用两种方式来处理它:周期性地在服务器上构建新的页面(和客户请求无关),或者仅仅在用户请求该页面时再构建。具体应该采用哪种方式要根据具体情况而定,但后一种方式常常更为方便,因为它只需简单地等待用户的请求。例如,天气预报或新闻网站可能会动态地构建页面,也有可能会返回之前构建的页面(如果它还是最新的话)。

3.页面中使用了来自公司数据库或其他数据库断数据源的信息。

如果数据存储在数据库中,那么,即使客户端使用动态Web内容,比如applet,我们依旧需要执行服务器端处理。想象一下,如果一个搜索引擎网站完全使用applet,那么用户将会看到:“正在下载50TB的applet,请等待!”。显然,这样很愚蠢;这种情况下,我们需要与数据库进行会话。从客户端到Web层再到数据库(三层结构),要比从applet直接到数据库(二层结构)更灵活,也更安全,而性能上的损失很少甚至没有。毕竟数据库调用通常是对速度影响最大的步骤,因而,经过中间层可以执行高速缓存和连接共享。

理论上讲,servlet并非只用于处理HTTP请求的Web服务器或应用服务器,它同样可以用于其他类型的服务器。例如,servlet能够嵌入到FTP或邮件服务器中,扩展它们的功能。而且,用于会话启动协议服务器的servletAPI最近已经标准化(参见http://jcp.org/en/jsr/detail?id=116)。但在实践中,servlet的这种用法尚不流行,在此,我们只论述HTTP Servlet。

1.3 Servlet相对于“传统”CGI的优点

和传统CGI及许多类CGI技术相比,Java Servlet更高效、更易用、更强大、更容易移植、更安全、也更廉价。

1.高效

应用传统的CGI,针对每个HTTP请求都用启动一个新的进程。如果CGI程序自身相对比较简单,那么启动进程的开销会占用大部分执行时间。而是用Servlet,Java虚拟机会一直运行,并用轻量级的Java线程处理每个请求,而非重量级的操作系统进程。类似的,应用传统的CGI技术,如果存在对同一CGI程序的N个请求,那么CGI程序的代码会载入内存N次。同样的情况,如果使用servlet,则启动N个线程,但是仅仅载入servlet类的单一副本。这种方式减少了服务器的内存需求,通过实例化更少的对象从而节省了时间。最后,当CGI程序结束对请求的处理后,结束程序。这种方式难以缓存计算结果,保持数据库连接是打开的,或是执行依靠持续性数据的其他优化。然而,servlet会一直停留在内存中(即使请求处理完毕),因而可以直接存储客户请求之间的任意复杂数据。

2.便利

Servlet提供大量的基础构造,可以自动分析和解吗HTML的表单数据,读取和设置HTTP报头,处理cookie,跟踪会话,以及其他此类高级功能。而在CGI中,大部分工作都需要我们资金完成。另外,如果你已经了解了java编程语言,为什么还要学Perl呢?您已经承认应用Java技术编写的代码要比Visual Basic,Vbscript或C 编写的代码更可靠,且更易重用,为什么还有倒退回去选择那些语言来开发服务器端的程序呢?

3.强大

Servlet支持常规CGI难以实现或根本不能实现的几项功能。Servlet能够直接与Web服务器对话,而常规的CGI程序做不到这一点,至少在不使用服务器专有API的情况下是这样。例如,与Web服务器的通信使得相对URL转换成具体的路径名变得更为容易。多个servlet还可以共享数据,从而易于实现数据库连接共享和类似的资源共享优化。Servlet还能维护请求之间的信息,使得诸如会话跟踪和计算结果缓存等技术变的更为简单。

4.可移植性

Servlet使用Java编程语言,并且遵循标准的API。所有主要的Web服务器,实际上都直接或通过插件支持servlet。因此,为Macromedia JRun编写的servlet,可以不经过任何修改地在Apache Tomcat, Microsoft Internet Information Server, IBM WebSphere, iPlanet Enterprise Server, Oracle 9i AS或者StrNine WebStar上运行。他们是java 2平台企业版的一部分,所以对servlet的支持越来越普遍。

5.廉价

对于开发用的网站、低容量或中等容量的网站的部署,有大量免费或极为廉价的Web服务器可供选择。因此,通过使用servlet和JSP,我们可以从免费或廉价的服务器开始,在项目获得初步的成功后,再移植到更高性能或高级管理工具的昂贵的服务器上,这与其他CGI方案形成鲜明对比,这些CGI方案在初期都需要购买专利软件包投入大量的资金。

价格和可移植性在某种程度上是相互关联的。例如,Marty记录了所有通过电子邮件想他发送问题的读者的所在国。印度接近列表的顶端,可能仅次于美国。Marty曾在马尼拉讲授过JSP和servlet培训课程,那儿对servlet和JSP技术抱有很大兴趣。

那么,为什么印度和菲律宾都对这项技术这么感兴趣呢?我们推测答案可能分两部分。首先这两个国家都拥有大量训练有素的软件开发人员。其次,这两个国家的货币对美元的汇率都极为不利。因此,从美国公司那里购买专用Web服务器会消耗掉项目的大部分前期资金。

但是,使用servlet和JSP,他们能够从免费的服务器开始:Apache Tomcat。项目取得成功之后,他们可以转移到性能更高的、管理更容易,但需要付费的服务器。它们的servlet和JSP不需要小心编写。如果他们的项目变得更庞大,他们或许希望转移到分布式环境。没有问题:他们可以转而使用Macromedia JRun Professional,该服务器支持分布式应用。同样,他们的servlet 和JSP没有任何部分需要重写。如果项目变得极为强大,错综复杂,他们或许希望使用Enterprise JavaBean来封装他们的商业逻辑。因此,他们可以切换到BEA WebLogic或Oracle 9i AS。同样,不需要对servlet和JSP进行修改。最后,如果他们的项目变得更强大,他们或许将它从Linux转移到运行IBM WebSphere的IBM大型机上,他们依旧还是不需要坐出任何更改。

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[29637],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图