【正文】
JavaServer 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 it39。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. A JSP technology overview Let39。s begin by talking a little about how JSP pages work. We39。re going to keep it 建立基于 Web 技術(shù)的郵件服務(wù)平臺(tái) 李治坤 II 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 WebSphere Application Server, will intercept requests for JSP pages. It39。s tipped off to their existence by the page39。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. We39。ll look at how to make this work in a minute. First, let39。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, 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. 大連民族學(xué)院計(jì)算機(jī)科學(xué)與工程系 2021 屆畢業(yè)論文 III The mechanics Let39。s walk through an example in which a static XML document is converted to a 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 WebSphere Transcoding Publisher called FlightInfo. 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 deviceappropriate 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. Here39。s the original XML document (the DTD has been omitted): …… Rename the file using a .jsp extension First, we need to rename the file with a .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 server39。s list for handling URLs and file extensions specific to the application server. When a Web server sees a file with a .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 piler what the format of the generated content will be. By default, the page piler will assume the content is HTML. To override this, a JSP page directive tag must be added to set the content