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>