Tianji.com published on May 2nd
JSP and XML combined with XML Only the interaction of JSP and XML only mentioned a method of using the tag library, and the JSP and XML interaction have two important methods. Therefore, in this article, I think of a brief elaboration, as "as"
JSP and XML Combination of XML.
Three different ways to use JavaServer Pages are used to process XML documents, each method facilitates the improvement of the level of separation page code and XML data, which is conducive to simplifying the complexity of the development web page and improving the reusability of increasing components and page code.
JavaServer Pages (JSP) and XML are two critical components of Sun's J2EE. JSP is a valid tool for creating application server-side programs, while customers can be a browser, a device, or other application. You can use XML to describe data and pass between contact servers and the rest of the system. If you carefully consider the abstract concept of the web service, JSP can be considered to implement technology and XML is data package and messaging technology. The JSP page can be used in three ways: use the XML file directly, use JavaBeans to perform XML processing or use XML through the tag library.
First, use XML directly
We can use XML directly in the JSP page, which is divided into three categories:
1. JSP can read the XML file and perform actions based on these data. For example, an application can read an XML file with some specific structures.
2. JSP can create an XML file to send data to a client or other application. JSP can convert an XML file, which can be handed over to XSLT processing, which is done by JSP as a controller, or through a non-XSLT solution. In both cases, JSP's role is to read an XML file, convert it and generate an output.
Because JSP contains embedded Java programs, it can call a analyst to read / write XML data directly. This is a very unreasonable method, because data and code logic do not be well separated. In addition, such a program is also difficult to read. So, I will introduce the second method: use JavaBeans.
Second, use javabeans
JSP can be tightly integrated with JavaBeans via
Last Name is:. B> <% = cb.getlname ()%>
The feature of JSP and JavaBeans integrated is to automatically translate form elements of hypertext logo language into JavaBeaN properties. If there is an HTML form and want it to submit form content to JavaBean, you can write the following code:
The Name property contains the value of the JSP page that has been referenced. The previous
These abstract movements that use JSP and JavaBeans are much better than inserting the original Java program directly in the JSP page, but you still need to be familiar with the Java program so you can change the JSP page at any time. The consistency and provision of applications rely on Javabeans to create a unified output result. For example, defects in Beans may cause the entire XML output to be invalid. Relying on Beans Specifying Resources Methods to implement performance issues.
Third, the interaction through the tag library JSP and XML is the focus on the previous article, but because it is too important, I have to mention it in this article. The tag library can define a custom label that appears as a class XML element in the JSP page, which can associate a specific Java code with each tag. For example, assuming that you can access a weather condition database, and you need to output the current weather conditions. So, you can insert the JDBC program code directly in the JSP to query the database (although this is not a good choice), package these code into a javabean, or package it into a tag library. Using the last option, the program code in your JSP page looks like:
<% @ Taglib Uri = "TLD File" prefix = "foo"%> Current weather is
Note that no trace of any Java code is not seen in the above program code. As a page designer, you use a familiar grammar like
The tag library has an associated XML format descriptor file, named Tag Library Descriptor (Tag Drawing Descriptor, TLD). Each tag in the TLD file has an entry, including its name, version, and other options. In the JSP page, you can specify a TLD file with the "<% @ _ taglib prefix =" foo "%>" instruction. The property "prefix" is used to specify a prefix to use a specific library to use any tags within the JSP page. Then why we want to use tag
A tag label can replace the corresponding Java program code for this program logic. Each tag is equivalent to a Java class that is the same name. This class must implement the Tagsupport interface, including the capture event trigger method as a JSP engine that handles this page. When it first encounters this tag, the engine calls the dostarttag () method. This method can be empty or the application logic is performed when needed. When the method returns Skip_Body, the engine skips this marker. When it returns EVAL_BODY_INCLUDE, the engine will process this tag and its sub-tag. Similarly, the JSP engine calls the DOENDTAG () method after analyzing the end tag. The DoafterBody () method allows you to perform actions after the engine processing element body, but must work before doendtag () method. Below is a sample program code of the Weather class for the 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 (); 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 "
The simplest method of using JSP and tag libraries is to use the Apache Tomcat Engine. This engine also acts as a reference to the servlet and JSP application interface.
When using the tag library, the JSP page looks very similar to the XML file. When the JSP page is processed, the engine executes program code associated with the tag (actually, first call the JSP engine to translate the JSP page into a servlet, then compile the servlet. The method associated with the tag library is included in the servlet .) A person familiar with XML can design and use a variety of page layouts without having to change any Java program code. Of course, the degree of separation of code and data is still mainly dependent on the quality of the marking element design.