7 _ PHP Tutorial of JAVA/JSP Learning Series

Source: Internet
Author: User
Tags define local tld
Seven of the JAVAJSP learning series. I. Preface (translated from Orion official site) This article describes how to define your own tag under OrionApplicationServer step by step, and then implement the tag function, finally, use a jsp example 1. 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 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 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)

Environment (translated from Orion official site) this article will introduce step by step define your own tag under Orion Application Server, then implement the tag function, and finally use a jsp example...

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.