Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JSP and XML interact

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how JSP and XML interact. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

There are three different methods for processing XML documents using JavaServer Pages. Each method helps to improve the level of separating page code from XML data, simplifies the complexity of developing web pages, and improves the reusability of components and page code.

JavaServer Pages JSP and XML are two crucial components of Sun's J2EE. JSP is an effective tool for creating server-side programs for applications, while customers can be a browser, a device, or other applications. You can use XML to describe the data and pass it between the contact server and the rest of the system. If you carefully consider the abstract concept of Web services, JSP can be thought of as an implementation technology and XML is a data encapsulation and messaging technology. JSP pages can use XML in three ways: using XML files directly, using JavaBeans to perform XML processing, or using XML through tag libraries.

First, use XML directly

We can use XML directly in the JSP page, which falls into three categories:

1. JSP can read XML files and perform actions based on that data. For example, an application can read XML files with data of certain structures.

2. JSP can create XML files to send data to clients or other applications. JSP can convert XML files, which can be handled by XSLT, with JSP as the controller, or through a non-XSLT solution. In both cases, the role of JSP is to read the XML file, transform it, and generate an output.

Because JSP contains an embedded Java program, it can directly call an analyzer to read / write xml data. This is a very unreasonable approach because data and code logic are not well separated. In addition, such a program is also difficult to read. So, next I'll introduce the second method: using JavaBeans.

Second, use JavaBeans

JSP can be closely integrated with JavaBeans through tagging. The following program snippet demonstrates how to use a JavaBean in the JSP page to set and get properties.

< 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 > Last Name is:. < / B > <% = cb.getLname ()% >

The integration of JSP and JavaBeans is characterized by the ability to automatically translate form elements of hypertext markup language into JavaBean attributes. If you have an HTML form and want it to submit its contents to JavaBean, you can write the following code:

< jsp:setProperty name= "cb" property= "*" / >

The name property contains the value that the JSP page already references Bean. The previous tag setting name is "cb". Instead of setting a separate Bean property, you can use an asterisk to mark the "all" attribute. The JSP page automatically maps the Bean attribute of the HTML form value to the same name. If you read each HTML form element and then call the Bean setting method for the corresponding property, the result will be the same.

As you can see, tags like XML allow JSP pages to access JavaBeans. By transforming as much encapsulated code as possible into reusable components (JavaBeans), we can optimize the code in JSP pages to a minimum. You can use a general parser, such as Xerces or JAXPI, to interact with XML files in a separate JavaBeans-and you can change the parser without changing the JSP page. In addition, Beans can also use XSLT to perform the conversion of XML files.

Using JSP and JavaBeans to do these abstract actions is much better than inserting the original Java program directly into the JSP page, but you still need to be familiar with the Java program in order to change the JSP page at any time. The consistency and organization of the application depends on the degree to which JavaBeans works together to create a unified output. For example, a defect in Bean may invalidate the entire XML output. The method of relying on Beans to specify resources can also cause performance problems.

Third, interact with XML through the tag library JSP

This is what was highlighted in the previous article, but because it is so important, I must also mention it in this article. Tag libraries can define custom tags that appear in JSP pages as XML elements of the class, and can associate specific Java code with each tag. For example, suppose you have access to a weather database, and you need to output current weather conditions. Then, you can insert JDBC code into JSP to query the database directly (although this is not a good choice), encapsulate the code in a JavaBean, or wrap it into a tag library. With a choice of * *, the program code in your JSP page looks like:

<% @ taglib uri= "the TLD file" prefix= "foo" > Current weather is < foo:Weather/ >

Note that there is no trace of Java code in the above program code. As a page designer, you use a familiar syntax like, these look very similar to any other tag. Insert it in the page where it contains the HTML string for the current weather condition.

The tag library has an associated XML format descriptor file called Tag Library Descriptor (tag Library descriptor, TLD). Each tag in the TLD file has an entry, including its name, version, and other optional information. Within the JSP page, you can specify the TLD file with the "" command. The attribute "prefix" is used to specify a prefix that uses a specific library to reference any tag within the JSP page. Then why should we use tags instead of just. The exact location of the TLD file depends on the application server you are using.

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 capture event trigger method as the JSP engine for processing the page. When it encounters this tag * the engine calls the doStartTag () method. You can make this method empty or execute the application logic when needed. When the method returns SKIP_BODY, the engine skips the tag body. When it returns EVAL_BODY_INCLUDE, the engine will process the tag and its child tags. Similarly, the JSP engine calls the doEndTag () method after parsing the closing tag. The doAfterBody () method allows you to perform an action after the engine processes the element body, but only before the doEndTag () method works. Here is a sample code for the Weather class that implements 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 = 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 "" tag, it searches the tag library for a class with the same name. If the doStartTag () method is implemented (in this case), it will be called. This makes the string contain the weather conditions that are adapted to the display. Because the method returns that the SKIP_BODY,JSP reader is moved to the end of the tag.

The easiest way to use JSP and tag libraries is to use the Apache Tomcat engine. This engine also acts as a reference implementation of the Servlet and JSP application interfaces.

When using tag libraries, JSP pages look a lot like XML files. When the JSP page is processed, the engine executes the program code associated with the tag (in fact, the JSP engine is called first to translate the JSP page into a servlet, and then the servlet is compiled. The methods associated with the tag library are included in the servlet.) Someone familiar with JSP and XML can design and experiment with a variety of page layouts without having to change any Java program code. Of course, the degree of separation between code and data mainly depends on the design of tag library elements.

Thank you for reading! This is the end of the article on "how JSP and XML interact". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report