Create your first JSP tag Library

Source: Internet
Author: User
Tags xml example tld apache tomcat
ArticleDirectory
    • Create your first JSP tag Library
    • Introduction

Create your first JSP tag Library

internal:
required audience
basic tag library structure
your first standard
describe your tag
Use your tag and
test materials

Introduction

Using assumerver pages to generate dynamic web pages can be divided into two ways: scripting & bean, which uses scripts to create Program Creation, and embed the JavaBean in it to process the parts of the bitwise operator. Another way is to use the tag library to replace the commonly used operators and operators with tags, it can even be used to parse the JDBC, javamail, and JMS. In this section, we will create your first JSP tag library, this tag library contains the most simple feature-it will display a string in your JSP interface. In the entire workflow, we first introduce the basic structure and operation process of a tag library, next, we will create a descriptive sub for this tag and deploy it to our web server. The last step is to determine whether the tag is running normally.

Required Samples

Webserver on the JSP interface:

In this tutorial, we use Apache Tomcat 4.1.12

Tag library Basic Structure

First, each tag we create must be implemented as the interface javax. servlet. jsp. tagext. Tag.

Tag conventions can be divided into two types: Body Tag or tag (when simple tag is used). In this article, we will use simple tag as signature.

The lifecycle of a tag marked as follows:

When the user asks for a JSP plane, the container begins to parse the JSP plane, and when it is resolved to a tag, the container will perform the following operations:

    • Use the setpagecontext () method of this tag to set the current pagecontext for later use.
    • Use the setparent () method to set the external tag resolved by the iner to include this tag (if not, null is entered ).
    • Set all written values defined in this tag.
    • Call dostarttag (). This method will return the eval_body_include or skip_body integer value. If the return value is eval_body_include, the content in the two tag (Body Tag) labels will be parsed. If the return value is skip_body, otherwise, the container will slightly overwrite the content of the tag.
    • Call doendtag (). This method will return the full value of eval_page or skip_page. When the returned value is eval_page, the container end with this mark, parse the JSP content of the remaining shard. When the return value is skip_page, the container will stop parsing the JSP content of the remaining shard after parsing this tag.
    • Finally, the release () method is called. You can use this method to release any resources used by this tag.

The following rule indicates the Exception Handling Mechanism of the tag Library:

When the dostarttag () or doendtag () method is called, a jsptagexception occurs in the container.

Your first metric

As this is our first benchmark, we will implement each method in the tag interface step by step, in addition, some other features provided by J2EE are not used for convenience at first (for example, tagsupport or bodytagsupport ).

First, add a 'helloworldtag 'under the WEB-INF/classes under your web project category (for example, "C: \ tomcat4.1.12 \ webapps \ taglib. java, which is explained below:

Import javax. servlet. JSP. *; import javax. servlet. JSP. tagext. *;/*** a simple tag that displays a hello World message. */public class helloworldtag implements tag {

First, we need to implement the javax. servlet. jsp. tagext. Tag interface.

Private pagecontext; private tag parent;

Add these two changes to store the current pagecontext and the tag of the external region.

/*** Constructor */Public helloworldtag () {super ();}

Implement the constructor of this mark.

/*** Method called at start of tag * @ return either a eval_body_include or a skip_body */Public int dostarttag () throws javax. servlet. jsp. jsptagexception {return skip_body ;}

Next, we implement the dostarttag () method and return the skip_body, which means we will not test the JSP content contained in this tag.

/*** Method called at end of tag * @ return either eval_page or skip_page */Public int doendtag () throws javax. servlet. JSP. jsptagexception {try {pagecontext. getout (). write ("Hello world! ");} Catch (Java. Io. ioexception e) {Throw new jsptagexception (" Io error: "+ E. getmessage ();} return eval_page ;}

The content above indicates that we use pagecontext to set "Hello world! "This string is returned to the client, that is, our JSP page, and we noticed that the value of our response is eval_page, indicates that the container completes the JSP content of the remaining worker. If we want this JSP to stop the display after the worker completes this label, the return value can be converted to skip_page.

/*** Method called to releases all resources */Public void release (){}

Because of this standard structure, we do not need to perform any operation on the source.

/** Method used by the JSP Container to set the current pagecontext * @ Param pagecontext, the current pagecontext */Public void setpagecontext (final javax. servlet. JSP. pagecontext) {This. pagecontext = pagecontext;}/** method used by the JSP Container to set the parent of the tag * @ Param parent, the parent tag */Public void setparent (final javax. servlet. JSP. tagext. tag parent) {This. parent = parent ;}

The preceding two methods are called by JSP iner and are used to set the pagecontext and external signature of this tag.

** Method for retrieving the parent * @ return the parent */Public javax. servlet. jsp. tagext. Tag getparent () {return parent ;}}

Finally, we also need to implement the getparent () method.

Next, please renew your program.

Describe your criteria

When your first metric is complete, we need a descriptor to describe this metric, so that your JSP page can recognize this mark:

  • Create a text comment 'taglib. TLD 'under your'/WEB-INF/'object'
  • Make the XML table header in the following column as the start
    <? XML version = "1.0" encoding = "ISO-8859-1"?> <! Doctype taglib public "-// Sun Microsystems, Inc. // dtd jsp tag library 1.1 //" http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd ">
  • Start to describe your tag Library)
    <Taglib> <tlibversion> 1.0 </tlibversion> <jspversion> 1.1 </jspversion> <shortname> mt </shortname> <URI> http://www2.tw.ibm.com/mytags.jar </uri> <info> my first tag library </INFO>
  • Start to describe your helloworld tag)
    <Tag> <Name> helloworld </Name> <tagclass> COM. acme. tag. helloworldtag </tagclass> <bodycontent> Empty </bodycontent> <info> A Hello World tag </INFO> </Tag>
  • End with your description
    </Taglib>

    Start and release the Web. xml example under '/WEB-INF/'. Add the following description at the end of this example

<Taglib> <taglib-Uri> mytags </taglib-Uri> <taglib-location>/WEB-INF/taglib. TLD </taglib-location> </taglib>

And use your tag

To add your tag, add the following JSP page to your web project:

<% @ Taglib uri = "mytags" prefix = "MT" %> <HTML> 

Finally, open your web server and load http: // localhost/taglib/helloworld. jsp

When you see the preceding labels, you have completed the first tag.

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.