Analysis of JSP and XML interactive programming skills (1)

Source: Internet
Author: User
Tags xslt

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.
 


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.