Combination of JSP and XML

Source: Internet
Author: User

Summary: eXtensible Markup Language (XML) is rapidly being used in the industry, it has been widely used as a format description and data exchange standard unrelated to platforms, languages and protocols. XML and its auxiliary specifications can be used to describe the document performance of data, describe the restrictions of XML document types, describe the links between XML documents and resources, and describe the automatic conversion and formatting of XML documents.

  How to Develop a custom tag library?

I have been using JSP and ASP programming for a long time. In the two programming methods on the server side, I feel that JSP functions are much more powerful. The tag library of JSP is the reason why I chose JSP as the preferred Server Web application development tool. Why? Because: the speed of maintenance and development. On a single server page, you can mix different script methods and objects. ? Like "concrete", this mixture can make the scripts on the server powerful and allow programmers on the server to design highly flexible and dynamic Web pages. However, this free mix also has its disadvantages, that is, it is very difficult to maintain, especially when the project grows bigger. Because the final product is maintained by a traditional Web designer, it may cause problems. What's worse, as the Code complexity increases, the development speed slows down, which is not conducive to the development of medium and large Web applications. Once the development is complete, the site also needs qualified programmers to maintain these complicated codes.
Fortunately, JSP provides a good solution. The tag Library provides a simple method to create a reusable code block. Once the tag library is designed, it can be used again in many projects. What's more convenient is that, unlike COM and J2EE, you can build a tag library without learning any other skills! As long as you know how to write JSP, you can create a tag library. The tag library can also improve the maintenance of Web applications. This is a simple XML interface that benefits from custom tags on JSP pages. In this way, Web designers can even build JSP Web applications without having to know any JSP knowledge. This open Web development is very effective for team operations. JSP programmers can create custom tags and background code modules. Web designers can use custom tags to create Web applications and focus on Web design.

  1. Tag library Definition
JSP tag Library (also called custom Library) can be seen as a set of methods for generating XML scripts, which are supported by JavaBeans. In terms of concept, the tag library is a very simple and reusable code structure.
Sample labels and HTML pages for executing XML/XSL Conversion

<% @ Taglib uri = "http://www.jspinsider.com/jspkit/JAXP" prefix = "JAXP" %>
C:/xml/example. xml
C:/xml/example. xsl

In this example, a simple tag is used to access more powerful code in the background. an XML file is loaded and an XSL file is used to generate a result, which is sent to the client concurrently, all is done by using a simple tag call.
Custom tags open a door for creating code that is easy to reuse in JSP projects. All you need is the tag library and its documentation.

   2. Tag Components
Although the tag library is very easy to use, it is quite complicated to build an internal design to support the tag library, at least more complicated than creating a simple JavaBean. This complexity comes from the fact that the tag library consists of several parts. However, you only need to know about Java and JSP.
A simple tag consists of the following elements:
(1) JavaBeans: To get the object-oriented benefits of Java and Java, reusable code should be put into an independent code container. These JavaBeans are not part of the tag library. However, it is the basic code block that your code library uses to execute related tasks.
(2) Tag processing: this is the true core of the tag library. A tag processor will reference any resources it needs (your JavaBeans) and all information that accesses your JSP page (pageContext object ). The JSP page also sends all configured tag attributes and content in the TAG body on the JSP page to the tag processor. After the tag processor completes processing, it will be sent back to your JSP page for processing.
(3) Description of the tag Library (tld file): this is a simple XML file that records the attributes, information, and locations of the tag processor. The JSP Container uses this file to learn where and how to call a tag library.
(4) website web. xml file: This is the initialization file of your website. In this file, you define the custom tag used in the website and the tld file used to describe each custom tag.
Dispatch file (a WAR or JAR file): If you want to reuse a custom tag, you need a method to transfer it from one project to another. It is a simple and effective method to package the tag library into a JAR file.
Declare the tag library in your JSP file: Very simple. If you want to use this tag, you only need to declare it on the page. Then, you can use it anywhere on the JSP page.
It seems that there is a lot of work to be done, but it is not very difficult. Its main point is not coding, but how to correctly organize all parts. However, such a hierarchy is very important, it can make the use of labels flexible and easier to transfer. More importantly, the existence of these layers can automatically complete the tag creation project through a jsp ide (integrated development environment of JSP. We hope that the jsp ide will automatically complete most of the work of creating a custom tag, so you only need to write code and tag processing.
Note: Only one custom tag is defined for one tag processing. A tag library is a collection of tag processors that process the same task.

This topic consists of four pages, currently on page 1 2 3 4


3. Create your own labels
The following describes how to create a custom tag step by step. The specific example is to expand JSP so that it has its own HTML encoding function. This function replaces all <and> characters with HTML code. It can be easily extended for other encoding processing. To simplify the process, this example only explains the basic elements for creating custom tags.
(1) create a JavaBean
Any reusable part of your code should be placed in a JavaBean. This is important because you often need to use the code elsewhere in the project. Any Code placed on the tag processor cannot be reused outside the tag. Therefore, it is important to separate reusable code. In this example, the logic for HTML encoding is commonly used, so it is placed in JavaBean.
(2) HTML-encoded JavaBean

/* HTML_Format.Java */
Public class HTML_Format extends Object implements Java. io. Serializable {
/** Create a new HTML_Format */
Public HTML_Format (){}
/** Replace all <and> characters in a string with the HTML encoding of the response */
Public String HTML_Encode (String as_data)
{
Int li_len = as_data.length ();
/* The length of string buffer must be longer than the original string */
StringBuffer lsb_encode = new StringBuffer (li_len + (li_len/10 ));
/* Replace all <and> characters in a loop */
For (int li_count = 0; li_count <li_len; li_count ++)
{String ls_next = String. valueOf (as_data.charAt (li_count ));
If (ls_next.equals ("<") ls_next = "<";
If (ls_next.equals (">") ls_next = "> ";
Lsb_encode.append (ls_next );
}
Return (lsb_encode.toString ());
}
}

(3) create a Tag Processor
The tag processor uses the following code:

HTML encoded tag processor
Import Java. io. IOException;
Import Javax. servlet. jsp .*;
Import Javax. servlet. jsp. tagext .*;
Public class HTML_FormatTag extends BodyTagSupport
{
/* 1} this function will be called at the end of the tag */
Public int doEndTag () throws JspTagException
{
Try
{/* 2} get the text in the tag */
BodyContent l_tagbody = getBodyContent ();
String ls_output = "";
/* 3} if the TAG body contains text, process it */
If (l_tagbody! = Null)
{HTML_Format l_format = new HTML_Format ();
/* 3a} convert the content of the TAG body to a string */
String ls_html_text = l_tagbody.getString ();
Ls_output = l_format.HTML_Encode (ls_html_text );
}
/* 4} write the result back to the data stream */
PageContext. getOut (). write (ls_output.trim ());
}
Catch (IOException e)
{Throw new JspTagException ("Tag Error:" + e. toString ());
}
/* Let JSP continue to process the content on the following page */
Return EVAL_PAGE;
}
}

This process is simple, including:
O read the text between the start and end of the tag
O call html encoding Functions
O return results to the JSP page.
(4) create a label Descriptor
You need to describe custom tags to let the system know how to handle them. The description file is suffixed with. tld, which is usually named the same as the tag processor and stored in the "/WEB-INF/" directory.

HTML encoded tag Descriptor
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE taglib
PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 // EN"
Http://Java.sun.com/j2ee/dtds/we

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.