JSTL access to SQL and XML content

Source: Internet
Author: User
js|xml| access to Web applications is a sign of integration of multiple subsystems. SQL and XML are two of the most common mechanisms for exchanging data between such subsystems. In this article, Mark Kolb introduces the SQL and XML libraries that access the JSP page database and XML content and summarizes the Jstl.
  
The template-type (stereotypical) architecture for Web applications is divided into three tiers: the Web server that handles the request, the application server that implements the business logic, and the database that manages the persistent data. Joins between applications and database tiers typically take the form of SQL calls in relational databases. JDBC is used to implement these calls when business logic is written to the Java language.
  
If an application invokes integration with other servers (local or remote), we will need a deeper mechanism for exchanging data between different subsystems. An increasingly common method of transferring data between and within a Web application is the exchange of XML files.
  
So far, in our jstl journey, we have discussed the JSTL expression language (expression Language,el) and the core and FMT tag libraries. In the last installment, we'll consider the SQL and XML libraries--as their names indicate--to provide custom tags to access and manage data retrieved from SQL databases and XML files.
  
   XML Library
By design, XML provides a flexible way to represent structured data that is prepared for validation, so it is particularly adaptable to exchanging data between loosely federated systems. This, in turn, makes it an attractive integration technology for Web applications.
  
The first step in interacting with data represented by XML is to retrieve and decompose data as an XML file to create data structures to access the contents of the file. After the file is exploded, you can optionally convert it to create a new XML file, and you can do the same for the new XML file. Eventually, the data in the file can be extracted and then displayed or used as input data to run other operations.
  
These steps are reflected in the JSTL tag that is used to control the XML. As we discussed in the 2nd part of the discussion, we use the <c:import> tag in the core library to retrieve the XML file. The file is then decomposed using the <x:parse> tag to support standard XML decomposition techniques, such as the file object schema (document objects Model,dom) and the simple XML API (Simplicity API for Xml,sax). <x:transform> tags can be used to transform XML files and rely on standard technology to transform XML data: Extended Stylesheet Language (extensible Stylesheet language,xsl). Finally, we provide multiple tags to access and control the decomposed XML data, but all of this relies on another standard-xml path language (XML path Language,xpath) To reference the contents of the decomposed XML file.
  
   decomposing XML
<x:parse> tags have multiple formats, depending on the type of decomposition the user wants. The most basic format for this operation uses the following syntax:
  
<x:parse xml= "expression" var= "name" scope= "Scope"
filter= "expression" systemid= "expression"/>
  
Of these five attributes, only the XML attribute is required, the value should be a string containing the XML file to be decomposed, or a java.io.Reader instance, through which the file to be decomposed can be read. In addition, you can use the following syntax to specify the files to be decomposed based on the subject content of the <x:parse> tag:
  
<x:parse var= "name" scope= "Scope"
filter= "expression" systemid= "expression" >
Body Content
</x:parse>
  
The Var and scope attributes specify the scoped variable that stores the decomposed file. Other tags in the XML library can then use this variable to run other operations. Note that when the Var and scope attribute exists, the data structure type used by Jstl to represent the decomposed file is implemented as a guide, so that the vendor can optimize it.
  
If the application needs to process the decomposed file provided by JSTL, it can use another format of &LT;X:PARSE&GT, which requires the decomposed file to stick with a standard interface. In this case, the syntax for the tag is as follows:
  
<x:parse xml= "expression" vardom= "name" scopedom= "Scope"
filter= "expression" systemid= "expression"/>
  
When you use this version of <x:parse>, the object that represents the decomposed XML file must use the Org.w3c.dom.Document interface. When you specify an XML file based on the subject content in <x:parse>, you can also use the Vardom and Scopedom properties to replace the Var and scope properties, as follows:
  
<x:parse vardom= "name" scopedom= "Scope"
filter= "expression" systemid= "expression" >
Body Content
</x:parse>
  
The other two properties filter and SystemID can achieve accurate control of the decomposition process. The Filter property prescribes an instance of the Org.xml.sax.XMLFilter class to filter the file before it is decomposed. This is especially useful if the file being decomposed is very large, but the current job requires only a small amount of content to be processed. The SystemID property represents the URI of the file to be decomposed and resolves any associated paths that appear in the file. This property is required when the decomposed XML file uses the associated URL to refer to other files or resources that need to be accessed in the decomposition process
  
Listing 1 shows the use of the <x:parse> tag, including the interaction with <c:import>. Here <c:import> tags are used to retrieve the RDF site Summary (RSS) Feedback for the well-known Slashdot web site, and then use <x:parse> to decompose XML files that represent RSS feeds. An implementation-oriented data structure that represents the decomposed file is saved to a variable named RSS (with a page range).
  
Listing 1:<x:parse> interaction with <c:import>
  
<c:import var= "RssFeed" url= "HTTP://SLASHDOT.ORG/SLASHDOT.RDF"/>
<x:parse var= "RSS" xml= "${rssfeed}"/>
  
   Convert XML
XML is converted through an XSL style sheet. Jstl uses the <x:transform> tag to support this operation. As in the case of <x:parse>,<x:transform> tags support a variety of different formats. The syntax for the <x:transform> most basic format is:
  
<x:transform xml= "expression" xslt= "expression"
Var= "name" scope= "Scope"
xmlsystemid= "expression" xsltsystemid= "expression" >
<x:param name= "expression" value= "expression"/>
...
</x:transform>
  
Here, the XML attribute prescribes the file to be converted, and the XSLT attribute defines the style sheet for this transformation. Both of these properties are necessary and the other attributes are optional.
  
The XML attribute value as,<x:transform> as the <x:parse> XML attribute can be a string that contains an XML file, or a reader that accesses such a file. In addition, it can take the form of an instance of the Org.w3c.dom.Document class or Javax.xml.transform.Source class. Finally, it can also be a variable value assigned using the Var or vardom property of the <x:parse> operation.
  
Also, you can include the XML file to be converted based on the body content of the <x:transform> action. In this case the,<x:transform> syntax is:
  
<x:transform xslt= "Expression"
Var= "name" scope= "Scope"
xmlsystemid= "expression" xsltsystemid= "expression" >
Body Content
<x:param name= "expression" value= "expression"/>
...
</x:transform>
  
In both cases, the XSLT property that prescribes the XSL style sheet should be a string, reader, or javax.xml.transform.Source instance.
  
If the Var attribute exists, the converted XML file is assigned to the corresponding scoped variable as an instance of the Org.w3c.dom.Document class. Typically, the scope property prescribes the range of such variables to be allocated.
  
The <x:transform> tag also supports storing the result of the transformation in an instance of the Javax.xml.transform.Result class, rather than as an instance of the org.w3c.dom.Document. If the Var and scope properties are omitted, the result object stipulates that the value,<x:transform> token as the results property will use the object to hold the resulting application of the style sheet. The changes in the two syntaxes that use the <x:transform> result property are described in Listing 2:
  
Listing 2: Syntax changes for,<x:transform> operations when using the result property to provide a Javax.xml.transform.Result instance
  
<x:transform xml= "expression" xslt= "expression"
result= "Expression"
xmlsystemid= "expression" xsltsystemid= "expression" >
<x:param name= "expression" value= "expression"/>
...
</x:transform>
  
<x:transform xslt= "Expression"
result= "Expression"
xmlsystemid= "expression" xsltsystemid= "expression" >
Body Content
<x:param name= "expression" value= "expression"/>
...
</x:transform>
  
Whether you use either of these <x:transform> formats, you must create the Javax.xml.transform.Result object individually from the custom tag. The object itself is provided as the value of the result property.
  
If neither the Var attribute nor the result property exists, the results of the conversion are simply inserted into the JSP page as a result of the processing <x:transform> operation. This is especially useful when the stylesheet is used to convert data from XML to HTML, as shown in Listing 3:
  
Listing 3: Directly displaying the transformed XML data on a JSP page
  
<c:import var= "RssFeed" url= "HTTP://SLASHDOT.ORG/SLASHDOT.RDF"/>
<c:import var= "rsstohtml" url= "/web-inf/xslt/rss2html.xsl"/>
<x:transform xml= "${rssfeed}" xslt= "${rsstohtml}"/>
  
In this case, use the <c:import> tag to read RSS feeds and the appropriate stylesheet. The output of the stylesheet is HTML and is displayed directly by ignoring the <x:transform> Var and result properties. Figure 1 shows the result of the instance:
  
   Figure 1: The output of Listing 3
    
The Xmlsystemid and Xsltsystemid properties,<x:transform> the same as the SystemID property of <x:parse> are used to parse the associated paths in the XML file. In this case, the Xmlsystemid property is applied to the file provided based on the markup's XML attribute value, and the Xsltsystemid property is used to parse the root

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.