The interaction between JSP and XML in the JSP and XML document published by skynet in May 2 only mentions one way to use the tag library. There are two important ways to interact between JSP and XML, therefore, in this article, I would like to briefly elaborate to supplement JSP and XML.
There are three different ways to use assumerver Pages to process XML documents. Each method helps improve the level of separating Page code from XML data, it simplifies the development of web pages and improves the reusability of components and Page code.
Assumerver Pages (JSP) and XML are two crucial components of Sun's J2EE. JSP is an effective tool for creating application server programs, and the customer can be a browser, a device, or other applications. You can use XML to describe the data and transmit it between the contact server and the rest of the system. If you carefully consider the abstract concept of Web Services, JSP can be considered implementation technology while XML is Data encapsulation and message transmission technology. JSP pages can use XML in three ways: directly using XML files, Using JavaBeans for XML processing, or using XML through the tag library.
1. directly use XML
We can directly use XML on JSP pages, which can be divided into three types:
1. JSP can read XML files and execute actions based on the data. For example, an application can read XML files with certain structures.
2. JSP can create XML files to send data to customer programs or other applications. JSP can convert XML files, which can be processed by XSLT. JSP can be used as the Controller or a non-XSLT solution. In both cases, JSP is used to read an XML file, convert it, and generate an output.
Because JSP contains embedded Java programs, it can directly call an analysis program to read/write xml data. This is unreasonable because data and code logic cannot be well separated. In addition, such a program is hard to understand. So next I will introduce the second method: Using JavaBeans.
Ii. use JavaBeans
JSP can be tightly integrated with JavaBeans through the <jsp: usebean> tag. The following program snippets demonstrate how to use a JavaBean on the JSP page to set and obtain attributes.
<JSP: usebean id = "CB" Scope = "session" class = "xmlrep. Customer"/> <JSP: setproperty name = "CB" property = "ID" value = "45"/> <B> first name is: </B> <% = CB. getfname () %> <P> <B> last name is:. </B> <% = CB. getlname () %> |
The feature of JSP and JavaBeans integration is that the form elements of hypertext mark language can be automatically translated into the JavaBean attribute. If you have an HTML form and want it to submit the form content to JavaBean, you can write the following code:
<JSP: setproperty name = "CB" property = "*"/> |
The name attribute contains the bean value that has been referenced on the JSP page. The preceding <JSP: usebean> tag is named "CB ". Unlike setting separate bean properties, you can use asterisks to mark "all" attributes. The JSP page automatically maps the bean attribute whose HTML form value is the same name. If you read each HTML form element and call the bean setting method of the corresponding attribute, the result will be the same.
As you can see, the XML-Like tag allows JSP pages to access JavaBeans. By transforming as many encapsulated code as possible into reusable components (JavaBeans), we can optimize the code on the JSP page to a minimum degree. You can use a common syntax analysis program, such as xerces or jaxpi, to interact with XML files in a single JavaBeans-and you can change the analysis program without modifying the JSP page. In addition, beans can use XSLT to convert XML files.
Using JSP and JavaBeans to complete these abstract actions is much better than simply inserting the original Java program into the JSP page, but you still need to be familiar with the Java program to change the JSP page at any time. The consistency and organization of applications rely on the cooperation of JavaBeans to create a unified output result. For example, bean defects may cause invalid XML output. The method that relies on beans to specify resources may also cause execution performance problems.
3. Interaction with XML through tag library JSP
This is what I mentioned earlier, but because it is too important, I must also mention it in this article. The tag library can define the custom tags that appear on the JSP page as XML-like elements, and can associate specific Java code with each tag. For example, suppose you can access a weather condition database and you need to output the current weather condition. Then, you can insert JDBC program code in JSP to directly query the database (although this is not a good choice) and encapsulate the code into a JavaBean, or wrap it into a tag library. With the last choice, the program code on your JSP page looks like:
<% @ Taglib uri = "the TLD file" prefix = "foo" %> Current weather is <FOO: Weather/> |
Note that no trace of Java code is found in the above Code. As a page designer, you use a familiar syntax like <foo: weather/>, which looks very similar to any other tag. Insert the HTML string that contains the current weather condition in the page.
The Tag Library has an associated XML Descriptor file named Tag Library Descriptor (Tag Library Descriptor, TLD ). Each tag in the TLD file has an entry, including its name, version, and other optional information. On the JSP page, you can use the "<% @ _ taglib prefix =" foo "%>" command to specify the TLD file. Attribute "prefix" is used to specify a prefix. It is used to reference any tag using a specific library on the JSP page. Why do we need to use the tag <foo: Weather/> instead of just <Weather/>. The exact location of TLD files depends on the application server in use.
A tag library tag can replace the corresponding Java program code to complete the program logic. Each tag is equivalent to a Java class with the same name. This class must implement the TagSupport interface, including the method for capturing event triggers as the JSP Engine for processing this page. When it encounters this mark for the first time, the engine will call the doStartTag () method. This method can be left empty or the application logic can be executed only when necessary. When this method returns SKIP_BODY, the engine skips the TAG body. When it returns EVAL_BODY_INCLUDE, the engine processes this mark and its sub-mark. Similarly, the JSP Engine calls the doEndTag () method after analyzing the end tag. The doAfterBody () method allows you to execute an action after the engine processes the element body, but it must be before the doEndTag () method works. The following is a sample program code for the Weather class that implements Weather conditions:
Import javax. servlet. jsp .*; Import javax. servlet. jsp. tagext .*; Import java. io .*; Public class Weather extends TagSupport { Public int doStartTag (){ Try { JspWriter out = pageContext. getOut (); Out. print ("sunny and cloudy mixed with" + "Rain and sunshine ."); } Catch (IOException e ){ System. out. println ("Error" + e ); } Return (SKIP_BODY ); } } |
When the engine encounters a "<somePrefix: Weather/>" tag, it searches for a class with the same name in the tag library. If the doStartTag () method is implemented (in this example), it will be called. This causes the string to contain information that adapts to the displayed weather conditions. Because the method returns the SKIP_BODY, the JSP reader moves to the end of the tag.
The simplest way to use JSP and tag libraries is to use the Apache Tomcat engine. This engine also serves as a reference implementation for servlet and JSP application interfaces.
When the tag library is used, the JSP page looks very like an XML file. When a JSP page is processed, the engine executes the program code associated with the tag (in fact, the JSP Engine is called to translate the JSP page into a Servlet and then compile the servlet. Methods associated with the tag library are included in the servlet .), A person familiar with XML can design and experiment with various page la s without changing any Java program code. Of course, the degree of separation between code and data is mainly dependent on the design of tag library elements.