Simple Example of JSP simpletagsupport.

Source: Internet
Author: User
Tags netbeans

I have been a programmer for more than a year. I wrote something for the first time:

Here is a simple JSP tag instance.

1. Java class:

Package com. hjglddok. Tag;

 

Import javax. servlet. jsp. jspwriter;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext. jspfragment;
Import javax. servlet. jsp. tagext. simpletagsupport;

Public class mytaghandler extends simpletagsupport {

Private string name;

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

@ Override
Public void dotag () throws jspexception {
Jspwriter out = getjspcontext (). getout ();

Try {

 

/** If you are using netbeans, except for the following sentence based on your needs, all others are automatically generated by netbeans.

* Out. println (string) prints the string content to the web page.

*/
Out. println (urlencoder. encode (getname (), "UTF-8 "));

 

Jspfragment F = getjspbody ();
If (F! = NULL) F. Invoke (out );

} Catch (Java. Io. ioexception ex ){
Throw new jspexception ("error in mytaghandler tag", ex );
}
}

}

 

2. TLD File Content

<Taglib>

<Tlib-version> 1.0 </tlib-version>
<Short-Name> my </short-Name>
<URI>/WEB-INF/TLDs/My </uri>
<Tag>
<Name> mytaghandler </Name>

<! -- Class path -->
<Tag-class> com. hjglddok. Tag. mytaghandler </Tag-class>
<Body-content> scriptless </body-content>
<Attribute>

<! -- Name attribute of mytaghandler class -->
<Name> name </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>

</Taglib>

 

3. Web. xml file configuration

<Web-app>

<JSP-config>
<Taglib>
<Taglib-Uri>/My </taglib-Uri>
<Taglib-location>/WEB-INF/TLDs/My. TLD </taglib-location>
</Taglib>
</JSP-config>

</Web-app>

 

4. Practical Application

<% @ Page contenttype = "text/html" pageencoding = "UTF-8" %>
<% @ Taglib uri = "/my" prefix = "my" %>

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> JSP page </title>
</Head>
<Body>
<H1> Hello world! </H1>
<My: mytaghandler name = "aguang"/>
</Body>
</Html>

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.