软件外文翻译_第1页
软件外文翻译_第2页
软件外文翻译_第3页
软件外文翻译_第4页
软件外文翻译_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、软件外文翻译XML and Java Server PagesAuthors: Papadias, Dimitris ,Egenhofer, Max J.From: Geoinformatica vol. 1, no. 3 p.251-254 December 2009Java 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 oth

2、er 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 dynami

3、cally 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 por

4、tions 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 examp

5、le, 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

6、 as part of the request for the JSP page. The result is pure XML.1 . The JSP technology overviewLet'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

7、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

8、'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 (JS

9、P 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 reque

10、ster 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 t

11、he 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 docume

12、nt? Why bother with JSP at all? The most important reason,1providing 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 con

13、tent. 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

14、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 containin

15、g 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 mechanicsLet's walk through an example in which a static XML document is converted to JSP page, and portions of its content are rewritten

16、 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

17、 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

18、using JSP extensionFirst, 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 applicat

19、ion 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 directiveNext, we need to indicate to

20、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

21、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 p

22、age like so:In this case, the page compiler will remove the page directive on the first line, leaving a blank line before the <?xml.?> 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 pa

23、ge 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 regardles

24、s of its placement in the page. For readability, though, you2may want to consider adding it as the second line, like this: 3.Add the Java codeNow 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 (<%.%>

25、). 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 c

26、ode 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 curl

27、y 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 min

28、utes 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. A

29、s 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

30、 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 t

31、he 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, N

32、SAPI 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 s

33、erver 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

34、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 ea

35、ch 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 componentsThe JSP syntax supports adding JavaBeans components to the page and accessing them like any other Java object. T

36、he special <jsp:useBean> tag is used to declare and instantiate the bean. The page compiler will use the attributes of the <jsp:useBean> tag to build the Java3code in the page servlet, which is necessary to instantiate the bean and populate it with data from the request.Because JavaBeans

37、 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

38、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 exa

39、mple. 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 <jsp:useBean> tag that defines and instantiates the bean IntegrationObject.FlightInfo as fl

40、ightInfo. 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 invoc

41、ation 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.ConclusionJavaServer Pages technology has been an open standard for several years and has been widely used for generating dynamic H

42、TML 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 represent

43、ations 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.

44、 Using JavaServer Pages technology provides a powerful tool for sending your data over the Web in XML quickly and easily4中文翻译XMLf口 JSPAuthors: Papadias, Dimitris Egenhofer, Max J.From: 地学信息 vol. 1, no. 3 p.251-254 December 2009JavaServer Pages (JSP)技术通常用于构建包含动态内容的HTML页面。但是您也可以使用这一技术生成其他格式(包括XML)的动态内

45、容。本文将用实例说明如何将 JSP 页面构建为 XML 文档模板,此模板是在请求时使用嵌在该页面Java 代码“填充”的。 中的Web应用程序开发人员传统上使用 JSP技术动态构建HTML方法是将Java代码包括在HTML 源代码中。但您知道可以使用同样的方法生成HTML 之外的动态内容吗 , 您可以实现这一点,而且方法比较简单。可以使用 XML 文档构建 JSP 页面,该 XML 文档将用作输出模板,然后替换必须基于基层业务逻辑动态生成的部分。为了生成文档的动态部分,您既可以使用直接编写在JSP 页面中的 Java 代码,也可以使用从该页面外部调用的 Java 代码。生成文档的哪些部分由您控

46、制。例如,您可以使用 Java 代码生成两个XML标记之间的数据,生成XML 文档树的各个部分(标记和数据) ,甚至可以生成整个文档。Java 代码被从页面中除去,并被加工成一个servlet( 称为页面 servlet) ,然后 Java 应用程序服务器将其作为 JSP 页面请求的一部分运行。得到的结果是纯 XML。1. JSP 技术概述让我们先对JSP 页面的工作方式作一些简单的讨论。我们将力求简单,只将注意力集中于一些基本的方面。有关详细信息,请参阅参考资料中到其他JSP 技术信息的链接。从传统意义上讲, JSP 页面与 HTML 页面很相似,只是多了一些标记。这些标记使设计人员能够将J

47、ava代码(不是JavaScript)嵌入到页面中。Web应用程序服务器 ( 如 IBM WebSphere Application Server) 将截取对 JSP 页面的请求。页面的扩展名.jsp(不是.html)向应用程序服务器暗示了这些标记的存在。Web应用程序服务器随后对JSP 页面进行预处理,提取其中的 JSP 标记和任何内嵌的Java代码,而只保留HTML提取出来的JSP标记和内嵌Java代码用来构建Java servlet(JSP 页面 servlet) , Java servlet 运行该代码并将结果插入到原页面中JSP标记所在的位置。得到的结果是纯HTML在请求浏览器看到任

48、何结果之前, Java 代码被剥离并在服务器上运行。我们可以将同样的原理应用于 XML 页面。在包含 XML 的 JSP 页面的请求者5( 可能是一个浏览器,也可能是某个企业对企业的应用程序 ) 看到 XML 之前,Java 代码被剥离JSP 页面并用来生成其他内容,生成的内容被插入到 JSP 标记原来所在的页面位置。这种特性使您能够精确地控制将新内容插入到什么位置,甚至可以精确到单个字符。过一会儿我们将考虑如何使用以上的特性。首先,让我们考虑为什么您可能会想到用JSP创建动态XML为什么不只是编写Java应用程序或servlet 来生成整个文档 , 为什么要费心去使用 JSP 呢 , 最重要

49、的原因是无须为每个请求重新生成静态内容是有意义的 (假定 XML 文档只有部分内容是动态的 ) 。通过使用 JSP 页面,页面内的静态XML 就可以充当一个模板,该模板是用动态内容填充的。 Java代码的任务仅仅是生成可能随时间变化的内容- 这是一种更有效的方法。非常现实的一个问题是, JSP 页面使您能够将不同开发人员负责的任务分开。特别是,它们允许您更好地将数据与视图分离开,从而允许您在不影响逻辑的情况下添加新表示。设想这样一个Java servlet ,它执行业务逻辑,并根据请求的性质将生成的结果重定向到适当的 JSP 页面。例如,当 servlet 检测到 WAP 电话浏览器发出请求时

50、,它就可以将数据重定向到一个包含 WML 的 JSP 页面。对于标准浏览器请求,它可以将数据重定向到包括HTML 的 JSP 页面。结构让我们剖析一个示例,该示例将一个静态XML 文档转换为一个JSP 页面,该文档的部分内容被重写为要动态生成。本例基于 IBM WebSphere TranscodingPublisher 附带的一个称为 FlightInfo 的 XML 样例。此 XML 文档表示一个特定航线的信息。Transcoding Publisher 将它作为一个说明如何使用该产品以适合设备的格式再现XML 数据的样例。但是在Transcoding Publisher 或其他任何应用程

51、序有机会处理该文档之前,我们希望动态构建它的某些内容。下面是原始的 XML 文档 ( 已将 DTD 省略 ):2. 使用 .jsp 扩展名重命名该文件首先,我们需要使用 .jsp 扩展名重命名该文件,以便Web 服务器知道如何处理它。Web服务器按一定的规则作用于某些 URL模式或文件扩展名,如.html或 .gif 。当将一个Web 应用程序服务器添加到一个Web 服务器中时,该应用程序服务器就会在Web 服务器列表中添加规则,以便处理特定于该应用程序服务器的 URL 和文件扩展名。当 Web 服务器看到一个带有.jsp 扩展名的文件时,它就会将此请求传递给Web 应用程序服务器进行处理。添

52、加页面指令下一步,我们需要把将要生成的内容的格式通知 JSP 页面编译器。缺省情况6下,页面编译器将假定内容的格式是HTML要覆盖这一设置,必须添加 JSP页面指令来设置内容类型。对于 JSP 1.0 ,内容类型页面指令类似以下的形式:重要的是要注意JSP 页面编译器只会除去组成标记及其内容的字符。如果将JSP 标记放在一个新行上,页面编译器将除去除换行符之外的任何字符 ( 因为换行符不是标记的一部分) 。例如,假定我们按以下方式将内容类型标记添加到新JSP页面中 :在这种情况下,页面编译器将除去第一行中的页面指令,并在<?xml.?>版本标记之前留下一个空行。在XML文档中,XM

53、L版本标记必须位于第一行,所以这个示例将在XML 分析程序中导致错误。要修正这个错误,请将此页面指令添加到第二行,或者将内容标记连接在XML 版本标记之后。实际上将页面指令放在文档中的什么位置并不重要,因为页面编译器会将此指令添加到所生成的页面servlet 的 service() 方法的开头,而不管它在页面中处在什么位置。尽管这样,为了具有较好的可读性,您可能希望将它添加为第二行,如下所示:3. 添加 Java 代码现在剩下要做的事情就是添加 Java 代码来定制 XML 的某些内容。我们将Java 代码添加到文件中的 scriptlet 标记 (<%.%>) 之间。页面编译器将

54、把这两个标记之间的任何内容解释为纯Java 代码,并不加修改地将它添加到所生成的页面 servlet 的 service() 方法中。scriptlet 标记内的所有代码都被按它们在 JSP 页面出现的次序添加到service() 方法中。添加的代码的作用域是整个页面,从而是整个service() 方法。因此,如果您在文件开头的一对scriptlet标记之间定义一个变量,则以后您可以在文件中使用一对完全不同的scriptlet标记引用这个变量。可以通过在变量定义两侧添加您自己的一对花括号(.)来强加一个作用域。这两个花括号甚至可以在一对scriptlet 标记之间开始,而在另一对 scriptlet 标记之间结束。在下面的示例中,我用一些使用 Calendar 类的 Java 代码来替换原始XML文件中的几个静态日期。这段代码在为几个XML 标记创建适当时间条目的同时,使用当前日期,并添加时和分。 Java 代码用粗体表示。添加行号是为了便于以后引用。请注意, cal 对象对整个页面都是全局的,尽管使用它的一对scriptlet 标记并不是定义它

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论