为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

软件外文翻译

2017-09-20 16页 doc 48KB 65阅读

用户头像

is_624976

暂无简介

举报
软件外文翻译软件外文翻译 XML and Java Server Pages Authors: Papadias, Dimitris ,Egenhofer, Max J. From:《Geoinformatica》vol. 1, no. 3 p.251-254 December 2009 Java Server Pages (JSP) technology is typically used for building HTML pages with dynamic content. But you can use this tec...
软件外文翻译
软件外文翻译 XML and Java Server Pages Authors: Papadias, Dimitris ,Egenhofer, Max J. From:《Geoinformatica》vol. 1, no. 3 p.251-254 December 2009 Java Server Pages (JSP) technology is typically used for building HTML pages with dynamic content. But you can use this technology to generate dynamic content in other formats as well, including XML. Using real examples, this article will show how to build a JSP page as an XML document template that is "filled in" at request time using Java code embedded in the page. Web application developers traditionally have used JSP technology to build HTML dynamically by including Java code in the HTML source. But did you know that you can use this same approach to generate dynamic content besides HTML? You can, and it's relatively simple. You can build a JSP page using an XML document that will serve as the template for the output, then replace the portions that must be generated dynamically based on the underlying business logic. You use Java code, either written directly within the JSP page or called externally from the page, to generate the dynamic portions of the document. You are in control of how much of that document is generated. For example, you can use Java code to generate data between XML tags, to generate portions of the XML document tree (both tags and data), or even to generate the entire document. The Java code is removed from the page, processed into a servlet (known as the page servlet) and run by the Java application server as part of the request for the JSP page. The result is pure XML. 1. The JSP technology overview Let's begin by talking a little about how JSP pages work. We're going to keep it simple and focus on some of the basics. For more information, see Resources for links to additional JSP technology information. In the traditional sense, JSP pages look very much like HTML pages, with a few extra tags. These tags allow the designer to embed Java code (not JavaScript) in the page itself. A Web application server, like the IBM Web Sphere Application Server, will intercept requests for JSP pages. It's tipped off to their existence by the page's extension: .jsp (not .html). The Web application server then preprocesses the JSP page, taking out the JSP tags and any embedded Java code, leaving only the HTML. The extracted JSP tags and embedded Java code are used to build a Java servlet (JSP page servlet) that runs the code and inserts the results back into the original page where the JSP tags used to be. The result is pure HTML. The Java is stripped out and run on the server before the requesting browser sees any result. We can apply the same principle to an XML page. Before the requester of the JSP page containing XML ever sees the XML (be it a browser or some other B2B application), the Java code is stripped out of the JSP page and used to generate additional content, which is inserted back into the page at the points where the JSP tags used to reside. This feature gives you the ability to control exactly where new content is to be inserted, down to the character. We'll look at how to make this work in a minute. First, let's consider why you might want to create dynamic XML using JSP. Why not simply write a Java application or servlet to generate the entire document? Why bother with JSP at all? The most important reason, 1 providing only portions of an XML document are dynamic, is that it makes sense not to regenerate that static content for every request. Using a JSP page, the static XML within the page acts as a template that is filled out by the dynamic content. Your Java code is tasked with generating only the content that might change over time -- a more efficient approach. As a practical matter, JSP pages allow you to separate tasks for which different developers will be responsible. In particular, they allow you to better separate the data from the view, allowing you to add new presentations without affecting the logic. Imagine having one Java servlet that performs the business logic and redirects the resulting data to an appropriate JSP page based on the nature of the request. For example, a servlet might redirect data to a JSP page containing WML when it detects a WAP phone browser making the request. It could also redirect the data to a JSP page containing HTML for standard browser requests. The mechanics Let's walk through an example in which a static XML document is converted to JSP page, and portions of its content are rewritten to be dynamically generated. The example is based on an XML sample shipped as part of the IBM Web Sphere Transcoding Publisher called Flight Info. This XML document represents information about a specific airline flight itinerary. Transcoding Publisher provides it as a sample to show how the product can be used to render the XML data in device-appropriate formats. But before Transcoding Publisher, or any other application, has a chance to manipulate the document, we want to build some of its content dynamically. Here's the original XML document (the DTD has been omitted): 2.Rename the file using JSP extension First, we need to rename the file with JSP extension so that the Web server will know what to do with the file. Web servers have rules for acting on certain URL patterns or file extensions, such as .html or .gif. When a Web application server is added to a Web server, the application server adds rules to the Web server's list for handling URLs and file extensions specific to the application server. When a Web server sees a file with JSP extension, it will pass that request on to the Web application server for processing. Add the page directive Next, we need to indicate to the JSP page compiler what the format of the generated content will be. By default, the page compiler will assume the content is HTML. To override this, a JSP page directive tag must be added to set the content type. For JSP 1.0, the content type page directive looks like this: It's important to note that the JSP page compiler will remove only the characters making up the tag and its contents. If you place a JSP tag on a new line, the page compiler will remove everything but the new line (because that is not part of the tag). For example, suppose we add the content type tag to our new JSP page like so: In this case, the page compiler will remove the page directive on the first line, leaving a blank line before the version tag. In XML documents, the XML version tag must be on the first line, so this example would cause an error in an XML parser. To remedy this, add the page directive to the second line, or concatenate the XML version tag with the content tag. It really doesn't matter where in the document the page directive is placed because the page compiler will add the instruction to the beginning of the resulting page servlet's service() method regardless of its placement in the page. For readability, though, you 2 may want to consider adding it as the second line, like this: 3.Add the Java code Now all that is left is to add the Java code to customize some of the content of the XML. We add the Java code to the file between scriptlet tags (<%...%>). The page compiler will interpret anything between these tags as pure Java code and will add it, unchanged, to the service() method of the resulting page servlet. All code within the scriptlet tags is added to the service() method in the order that it appears within the JSP page. The scope of the code added is the entire page, and thus the entire service() method. So if you define a variable in one set of scriptlet tags at the beginning of the file, you can reference it later in the file using a completely different set of scriptlet tags. You can impose a scope by adding your own sets of curly braces ({...}) around variable definitions. These braces can even begin and end in different sets of scriptlet tags. In the following example, I replace a few of the static dates in the original XML file with some Java code using the Calendar class. The code uses the current date and adds hours and minutes along the way to create the appropriate time entries for several of the XML tags. The Java code is boldfaced. Line numbers are added for reference later. Note that the cal object is global to the entire page, even though it is used in a different set of scriptlet tags from where it is defined. As I mentioned before, unless you add braces ({...}) to enforce scope, variable declarations are global to the entire page from the point of declaration. Also, the Java code in this example is used primarily to generate data between XML tags. We could also use Java code to generate entire trees of tags in an XML document. The code must simply print out the tags in addition to the data. The Java Servlet is a technical foundation of JSP, and the large Web applies the development of the procedure to need the Java Servlet to match with with the JSP and then can complete, this name of Servlet comes from the Applet, the local translation method of now is a lot of, this book in order not to misconstruction, decide the direct adoption Servlet but don't do any translation, if reader would like to, can call it as" small service procedure". The Servlet is similar to traditional CGI, ISAPI, NSAPI etc. Web procedure development the function of the tool in fact, at use the Java Servlet hereafter, the customer need not use again the lowly method of CGI of efficiency, also need not use only the ability come to born page of Web of dynamic state in the method of API that a certain fixed Web server terrace circulate. Many servers of Web all support the Servlet, even not support the Servlet server of Web directly and can also pass the additional applied server and the mold pieces to support the Servlet. Receive benefit in the characteristic of the Java cross-platform, the Servlet is also a terrace irrelevant, actually, as long as match the norm of Java Servlet, the Servlet is complete to have nothing to do with terrace and is to have nothing to do with server of Web. Because the Java Servlet is internal to provide the service by the line distance, need not start a progress to the each claimses, and make use of the multi-threading mechanism can at the same time for several claim service, therefore the efficiency of Java Servlet is very high. Adding JavaBeans components The JSP syntax supports adding JavaBeans components to the page and accessing them like any other Java object. The special tag is used to declare and instantiate the bean. The page compiler will use the attributes of the tag to build the Java 3 code in the page servlet, which is necessary to instantiate the bean and populate it with data from the request. Because JavaBeans components are generically meant to perform a task, it's easy to see how they can be used to perform or interface with complex business logic, moving the complex Java code out of the JSP page. One example is an Integration Object JavaBean component. This bean was built using IBM WebSphere Host Publisher, which encapsulates interactions with legacy data sources, such as 3270 applications or databases, and simply returns their data. Imagine using an Integration Object that interfaces with a host-based airline scheduling application to provide the arrival and departure information in our example. As a user of the Integration Object, you simply request the itinerary data from it, unaware of the complex communication and interactions with the host application that occur in the background. The tag that defines and instantiates the bean IntegrationObject.FlightInfo as flightInfo. The next line contains a scriptlet that calls the doHPTransaction() method of flightInfo, causing it to connect to the host flight information application and retrieve pertinent flight information. You can use other types of beans in exactly the same manner, perhaps calling some other invocation method besides doHPTransaction() to do the work. You can even add multiple beans to the same document, creating a composite XML view of data from multiple sources. Conclusion JavaServer Pages technology has been an open standard for several years and has been widely used for generating dynamic HTML documents. Few have realized that the same flexibility of embedding actual Java code within an HTML document can be applied to generating XML. Consider the possibilities. You could streamline B2B communications by dynamically building only the data that changes. You might build various representations of data from one data source, not just HTML and XML, but WML and HDML for wireless protocols, or simply use products like WebSphere Transcoding Publisher to convert your dynamic XML to device-appropriate representations. XML clearly has been embraced as the common format for data interchange. Using JavaServer Pages technology provides a powerful tool for sending your data over the Web in XML quickly and easily 4 中文翻译 XML和JSP Authors: Papadias, Dimitris Egenhofer, Max J. From:《地学信息》vol. 1, no. 3 p.251-254 December 2009 JavaServer Pages (JSP) 技术通常用于构建包含动态的 HTML 页面。但是您也可以使用这一技术生成其他格式(包括 XML)的动态内容。本文将用实例说明如何将 JSP 页面构建为 XML 文档模板,此模板是在请求时使用嵌在该页面 Java 代码“填充”的。 中的 Web 应用程序开发人员传统上使用 JSP 技术动态构建 HTML,方法是将 Java 代码包括在 HTML 源代码中。但您知道可以使用同样的方法生成 HTML 之外的动态内容吗,您可以实现这一点,而且方法比较简单。可以使用 XML 文档构建 JSP 页面,该 XML 文档将用作输出模板,然后替换必须基于基层业务逻辑动态生成的部分。为了生成文档的动态部分,您既可以使用直接编写在 JSP 页面中的 Java 代码,也可以使用从该页面外部调用的 Java 代码。 生成文档的哪些部分由您控制。例如,您可以使用 Java 代码生成两个 XML 标记之间的数据,生成 XML 文档树的各个部分(标记和数据),甚至可以生成整个文档。 Java 代码被从页面中除去,并被加工成一个 servlet(称为页面 servlet),然后 Java 应用程序服务器将其作为 JSP 页面请求的一部分运行。得到的结果是纯 XML。 1.JSP 技术概述 让我们先对 JSP 页面的工作方式作一些简单的讨论。我们将力求简单,只将注意力集中于一些基本的方面。有关详细信息,请参阅参考资料中到其他 JSP 技术信息的链接。 从传统意义上讲,JSP 页面与 HTML 页面很相似,只是多了一些标记。这些标记使人员能够将 Java 代码(不是 JavaScript)嵌入到页面中。Web 应用程序服务器(如 IBM WebSphere Application Server)将截取对 JSP 页面的请求。页面的扩展名 .jsp(不是 .html)向应用程序服务器暗示了这些标记的存在。Web 应用程序服务器随后对 JSP 页面进行预处理,提取其中的 JSP 标记和任何内嵌的 Java 代码,而只保留 HTML。提取出来的 JSP 标记和内嵌 Java 代码用来构建 Java servlet(JSP 页面 servlet),Java servlet 运行该代码并将结果插入到原页面中 JSP 标记所在的位置。得到的结果是纯 HTML。在请求浏览器看到任何结果之前,Java 代码被剥离并在服务器上运行。 我们可以将同样的原理应用于 XML 页面。在包含 XML 的 JSP 页面的请求者 5 (可能是一个浏览器,也可能是某个企业对企业的应用程序)看到 XML 之前,Java 代码被剥离 JSP 页面并用来生成其他内容,生成的内容被插入到 JSP 标记原来所在的页面位置。这种特性使您能够精确地控制将新内容插入到什么位置,甚至可以精确到单个字符。 过一会儿我们将考虑如何使用以上的特性。首先,让我们考虑为什么您可能会想到用 JSP 创建动态 XML。为什么不只是编写 Java 应用程序或 servlet 来生成整个文档,为什么要费心去使用 JSP 呢,最重要的原因是无须为每个请求重新生成静态内容是有意义的(假定 XML 文档只有部分内容是动态的)。通过使用 JSP 页面,页面内的静态 XML 就可以充当一个模板,该模板是用动态内容填充的。Java 代码的任务仅仅是生成可能随时间变化的内容 -- 这是一种更有效的方法。 非常现实的一个问题是,JSP 页面使您能够将不同开发人员负责的任务分开。特别是,它们允许您更好地将数据与视图分离开,从而允许您在不影响逻辑的情况下添加新表示。设想这样一个 Java servlet,它执行业务逻辑,并根据请求的性质将生成的结果重定向到适当的 JSP 页面。例如,当 servlet 检测到 WAP 电话浏览器发出请求时,它就可以将数据重定向到一个包含 WML 的 JSP 页面。对于浏览器请求,它可以将数据重定向到包括 HTML 的 JSP 页面。 结构 让我们剖析一个示例,该示例将一个静态 XML 文档转换为一个 JSP 页面,该文档的部分内容被重写为要动态生成。本例基于 IBM WebSphere Transcoding Publisher 附带的一个称为 FlightInfo 的 XML 样例。此 XML 文档表示一个特定航线的信息。Transcoding Publisher 将它作为一个说明如何使用该产品以适合设备的格式再现 XML 数据的样例。但是在 Transcoding Publisher 或其他任何应用程序有机会处理该文档之前,我们希望动态构建它的某些内容。 下面是原始的 XML 文档(已将 DTD 省略): 2.使用 .jsp 扩展名重命名该文件 首先,我们需要使用 .jsp 扩展名重命名该文件,以便 Web 服务器知道如何处理它。Web 服务器按一定的作用于某些 URL 模式或文件扩展名,如 .html 或 .gif。当将一个 Web 应用程序服务器添加到一个 Web 服务器中时,该应用程序服务器就会在 Web 服务器列表中添加规则,以便处理特定于该应用程序服务器的 URL 和文件扩展名。当 Web 服务器看到一个带有 .jsp 扩展名的文件时,它就会将此请求传递给 Web 应用程序服务器进行处理。 添加页面指令 下一步,我们需要把将要生成的内容的格式通知 JSP 页面编译器。缺省情况 6 下,页面编译器将假定内容的格式是 HTML。要覆盖这一设置,必须添加 JSP 页面指令来设置内容类型。对于 JSP 1.0,内容类型页面指令类似以下的形式: 重要的是要注意 JSP 页面编译器只会除去组成标记及其内容的字符。如果将 JSP 标记放在一个新行上,页面编译器将除去除换行符之外的任何字符(因为换行符不是标记的一部分)。例如,假定我们按以下方式将内容类型标记添加到新 JSP 页面中: 在这种情况下,页面编译器将除去第一行中的页面指令,并在 版本标记之前留下一个空行。在 XML 文档中,XML 版本标记必须位于第一行,所以这个示例将在 XML 分析程序中导致错误。要修正这个错误,请将此页面指令添加到第二行,或者将内容标记连接在 XML 版本标记之后。实际上将页面指令放在文档中的什么位置并不重要,因为页面编译器会将此指令添加到所生成的页面 servlet 的 service() 方法的开头,而不管它在页面中处在什么位置。尽管这样,为了具有较好的可读性,您可能希望将它添加为第二行,如下所示: 3.添加 Java 代码 现在剩下要做的事情就是添加 Java 代码来定制 XML 的某些内容。我们将 Java 代码添加到文件中的 scriptlet 标记 (<%...%>) 之间。页面编译器将把这两个标记之间的任何内容解释为纯 Java 代码,并不加修改地将它添加到所生成的页面 servlet 的 service() 方法中。 scriptlet 标记内的所有代码都被按它们在 JSP 页面出现的次序添加到 service() 方法中。添加的代码的作用域是整个页面,从而是整个 service() 方法。因此,如果您在文件开头的一对 scriptlet 标记之间定义一个变量,则以后您可以在文件中使用一对完全不同的 scriptlet 标记引用这个变量。可以通过在变量定义两侧添加您自己的一对花括号 ({...}) 来强加一个作用域。这两个花括号甚至可以在一对 scriptlet 标记之间开始,而在另一对 scriptlet 标记之间结束。 在下面的示例中,我用一些使用 Calendar 类的 Java 代码来替换原始 XML 文件中的几个静态日期。这段代码在为几个 XML 标记创建适当时间条目的同时,使用当前日期,并添加时和分。Java 代码用粗体表示。添加行号是为了便于以后引用。 请注意,cal 对象对整个页面都是全局的,尽管使用它的一对 scriptlet 标记并不是定义它的同一对 scriptlet 标记。如前所述,除非添加花括号 ({...}) 来强制使用作用域,否则变量声明从声明之处对于整个页面都是全局的。 同样,本例中的 Java 代码主要用来生成 XML 标记之间的数据。我们还可以使用 Java 代码生成一个 XML 文档中的整个标记树。代码除了显示数据之外,还 7 必须简单地显示出标记。 Java Servlet是JSP技术的基础,而且大型的Web应用程序的开发需要Java Servlet和JSP配合才能完成,Servlet这个名称源于Applet,现在国内的翻译方式很多,本书为了避免误会,决定直接采用Servlet而不做任何翻译,读者如果愿意,可以称之为“小服务程序”。Servlet其实和传统的CGI、ISAPI、NSAPI等Web程序开发工具的作用是相似的,在使用Java Servlet以后,用户不必再使用效率低下的CGI方式,也不必使用只能在某个固定Web服务器平台运行的API方式来动态生成Web页面。许多Web服务器都支持Servlet,即使不直接支持Servlet的Web服务器也可以通过附加的应用服务器和模块来支持Servlet。得益于Java的跨平台的特性,Servlet也是平台无关的,实际上,只要符合Java Servlet,Servlet是完全与平台无关且是与Web服务器无关的。由于Java Servlet内部是以线程方式提供服务,不必对于每个请求都启动一个进程,并且利用多线程机制可以同时为多个请求服务,因此Java Servlet效率非常高。 添加 JavaBeans 组件 JSP 语法支持将 JavaBeans 组件添加到页面中,并像访问任何其他 Java 对象一样访问它们。专用的 标记用来声明和实例化 bean。页面编译器将使用 标记的属性来构建页面 servlet 中的 Java 代码,这对于实例化 bean 以及将请求中的数据置入 bean 是必需的。 因为 JavaBeans 组件通常必须执行一项任务,所以很容易看出如何使用它们来执行或干预复杂的业务逻辑,同时也从 JSP 页面中移去复杂的 Java 代码。一个示例是 Integration Object JavaBean 组件。这个 bean 是使用 IBM WebSphere Host Publisher 构建的,它封装了与旧有数据源(如 3270 应用程序或数据库)的交互,并只是返回它们的数据。设想使用一个 Integration Object,为了在我们示例中提供到达和出发信息,它与基于主机的航空调度应用程序对接。作为 Integration Object 的一个用户,您只须请求它的航线数据,而不会察觉到在后台发生的与主机应用程序之间的复杂通信和交互。 标记它将bean IntegrationObject.FlightInfo 定义并实例化为 flightInfo。下一行包含一个 scriptlet,它调用 flightInfo 的 doHPTransaction() 方法,使它连接到主机航班信息应用程序并检索相关的航班信息。 您可以按完全相同的方式使用其他类型的 bean,也许以调用 doHPTransaction() 之外的某个其他调用方法完成这一操作。您甚至可以在同一个文档中添加多个 bean,并为来自多个数据源的数据创建一个复合的 XML 视图。 8 小结 JavaServer Pages 技术成为一个开放标准已经几年了,并已广泛用于生成动态 HTML 文档。少数人已经认识到将实际的 Java 代码嵌在 HTML 文档中所具有的同等灵活性也适用于生成 XML。请考虑这种可能性。通过只动态构建那些变化的数据,您就能够改进企业对企业的通信。您可以构建来自一个数据源的数据的各种表示,不仅仅是 HTML 和 XML,还有用于无线协议的 WML 和 HDML,或者简单使用类似 WebSphere Transcoding Publisher 这样的产品将动态 XML 转换为适合设备的表示。 XML 无疑已成为数据交换的通用格式。使用 JavaServer Pages 技术为以 XML 格式在 Web 上快速而容易地发送数据提供了强有力的工具。 9
/
本文档为【软件外文翻译】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索