Analyzing the interactive programming skills of JSP and XML

Source: Internet
Author: User
Tags abstract html form web services xslt

JavaServer Pages (JSP) and XML are the two crucial components of Sun's Java EE. JSP is an effective tool for creating application server-side programs, and customers can be a browser, a device, or another application. You can use XML to describe the data and pass it between the contact server and the rest of the system. If you consider the abstract concept of Web services carefully, JSP can be considered as implementing technology while XML is 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, using XML directly

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

1. JSP can read XML files and perform actions based on these data. For example, an application can read an XML file that has some specific structure of data.

2. JSP can create an XML file to send data to a client program or other application. A JSP can transform an XML file, which can be given to XSLT processing, either as a controller by a JSP, or through a non-XSLT solution. In both cases, the function of the JSP is to read the XML file, convert it, and generate an output.

Because the JSP contains embedded Java programs, it can directly invoke an analyzer to read/write XML data. This is a very unreasonable method, because the data and code logic are not very good to be separated away. In addition, such procedures are difficult to read. So, I'll introduce the second method: Using JavaBeans.

Ii. Use of JavaBeans

JSP can be tightly integrated with JavaBeans through the < jsp:usebean> tag. The following fragment demonstrates how to use a JavaBean in a 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>
< B> Last Name is: .< /B>
< %= cb.getLname() %>

The feature of JSP and JavaBeans integration is that it can automatically translate the form elements of Hypertext Markup language into JavaBean attributes. If you have an HTML form and you want it to submit the form content to JavaBean, you can write the following code:

< Jsp:setproperty name= "CB" property= "*"/>

The Name property contains the value that the JSP page already refers to the bean. Front < jsp:usebean> tag Setting name is "CB". Unlike setting individual bean properties, you can use the asterisk to flag the "all" property. The JSP page automatically maps an HTML form value to a bean property with the same name. If you read each HTML form element and then call the corresponding property's bean set method, the result will be the same.

As you can see, the markup for class XML allows JSP pages to access JavaBeans. By turning the encapsulated code into a reusable component (JavaBeans) as much as possible, we can optimize the code in the JSP page to the minimum level. You can use a generic parser, like Xerces or JAXPI, to interact with an XML file in a separate JavaBeans-and you can change the parser without changing the JSP page. In addition, beans can use XSLT to perform transformations 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 familiarize yourself with the Java program so that you can change the JSP page at any time. Application consistency and bar rationality depends on JavaBeans cooperation to create a unified output of the good and bad degree. For example, a flaw in a bean can cause the entire XML output to be invalid. Depending on the method that beans specifies the resource, it can also cause performance problems.

Related Article

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.