Basic steps:
1. First write the class, extends tagsupport class, and rewrite the method you need.
(Note: If you inherit the simpletagsupport class, you must put all the processing code in the dotag () method)
2. Configuration
Write a TLD file and configure a web. xml file
3. jsp call
Note: The example provided by Tomcat is a good imitation example.
If the class inherits from simpletagsupport, all operations must be placed in the dotag () method;
If the class inherits from bodytagsupport,
In bodytagsupport, dostarttag () is reset back to eval_body_buffered, and then the setbodycontent () and doinitbody () methods are executed. setbodycontent () sets the bodycontent object, it includes some standard text information, and the objects that export data to response in the processing process, after which it will enter doafterbody () you can restore the eval_body_again or skip_body.
If you inherit the simpletagsupport class, you can use the built-in example of tamcat.
Public class pagedivide extends bodytagsupport {
Public iterator;
Public object element;
Public int dostarttag () throws jspexception {
Try {
Database DB = new database ();
DB. INIT ();
Iterator = dB. getcourse (). iterator ();
} Catch (exception e ){
E. printstacktrace ();
}
If (iterator. hasnext ()){
Element = iterator. Next ();
Pagecontext. setattribute ("course", element );
}
Return eval_body_buffered;
}
Public int doafterbody (){
Try {
Bodycontent. writeout (this. getpreviousout ());
} Catch (ioexception e ){
E. printstacktrace ();
}
Bodycontent. clearbody ();
If (iterator. hasnext ()){
Element = iterator. Next ();
Pagecontext. setattribute ("course", element );
Return eval_body_again;
} Else
Return eval_page;
}
}