Java/jsp Learning Series 7

Source: Internet
Author: User
Tags define local tld


I. Preface (translated from Orion official site)

This article will introduce how to define your own tag under Orion Application Server step by step, implement the tag function, and finally use a JSP Example for testing.

2. Create a tag to implement the tag function.

1. Name package: COM. Acme. mytags (to be the same as the original one, I will not change it)

Package com. Acme. mytags;

2. Import related class

Import javax. servlet. jsp .*;

Import javax. servlet. jsp. tagext .*;

3. Implement the javax. servlet. jsp. tagext. Tag interface:

Public class helloworld implements tag

{

4. Define local variables

Private pagecontext;

Private tag parent;

5. Implementation Method for calling the label

Public int dostarttag () throws javax. servlet. jsp. jspexception

{

Return skip_body;

}

Note: skip_body is returned in this method. If the label body is empty, this value is returned. Otherwise, the value eval_body_include is returned.

6. Call the implementation method of tag termination

Public int doendtag () throws javax. servlet. jsp. jspexception

{

Try

{

Pagecontext. getout (). Write ('Hello world! ');

}

Catch (Java. Io. ioexception E)

{

Throw new jspexception ('IO error: '+ E. getmessage ());

}

Return eval_page;

}

In this way, "Hello World!" will be output at the end of the custom tag of the JSP file !"

7. Not enough. We need to write down the following method:

Public void release (){}

For this simple example, the above method does not require any implementation.

8. The JSP Container needs to call the following method:

Public void setpagecontext (final javax. servlet. jsp. pagecontext ){

This. pagecontext = pagecontext;

}

The JSP Container uses the preceding method to call the tag, and the preceding method is used to set the pagecontext of the tag.

9. The JSP Container also calls the following method:

Public void setparent (final javax. servlet. jsp. tagext. Tag parent)

{

This. Parent = parent;

}

The JSP Container uses the preceding method to set the parent-tag of the tag, because the pagecontext of each tag must retain its parent tag.

10. Finally, the implementation method is as follows:

Public javax. servlet. jsp. tagext. Tag getparent ()

{

Return parent;

}

}

11. Compile tags.

Iii. descriptive tags

Now we will write an Tracing file to describe the tag.

1. Create a taglib. TLD file,

2. taglib. TLD is an XML text file. The xml header is as follows:

'Http: // java.sun.com/j2ee/dtds/web-jsptaglibrary_00001.dtd'>

3. Describe the tag Library

1.0

1.1

Mt http://www.orionserver.com/tutorials/tagtut/lesson1/mytags.jar

My first tag Library

4. descriptive tag

Helloworld

Com. Acme. mytags. helloworld

Empty

A Hello World tag

5. End

Iv. Packaging

Name: mytags. Jar

Its directory structure is:

COM/Acme/mytags/helloworld. Class

META-INF/taglib. TLD

5. Use custom tags in JSP files

Create hello. jsp as follows:

Vi. Test Run

In the Orion directory, create the following structure, where the tag is created by yourself, and the preceding directory is originally available.

E: oriondefault-web-app AG

Put all jar files and JSP files in this directory.

Then, access:

Http: // localhost: [port]/Tag/Hello. jsp

:
------------------------------------------------------------------
Hello world!
------------------------------------------------------------------

7. Congratulations, you have succeeded!

Appendix: mytags. jar and hello. jsp files site:

Http://www.wodejia.net/softdownload/java/orion_tag01.zip (Source: viphot.com)

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.