Skills for parsing JSP and XML Interactive Programming

Source: Internet
Author: User
Tags xslt tld

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. So why should we use the flag <foo: Weather/> instead of <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 the "<somePrefix: Weather/>" mark, 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.

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.

  1. Session Value Sharing between JSP and ASP. Net
  2. JSP combined with XML + XSLT to convert output to HTML
  3. Methodology in JSP project design

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.