I. Overview
There is an important technique in JSP: Custom Tag, which has been found in Struts, such as HTML, beans, etc. in Struts in recent days. So I have made a simple trial and learn this technology.
First introduce this technology!
1. advantage:
Replaced the JAVA program in JSP and can be reused, which is easy to familiarize with Java programming web designers.
2. Development Process:
(1) Write JSP and use a custom label in JSP.
(2) The location of the TD (Label Description File) file used in the JSP in the JSP is specified in Web.xml.
(3) The class specifies the class used in TLD file.
3. Customized tag classification:
(1) Simple tag: such as
(2) With attribute tags: such as
(4) Tags that can be used by Script:
The label defined by the id and Type property can be used by the scriptlet behind the label.
<% ORADB.GETCONNECTION ();%>
4. Interface and other
In fact, the custom label processing class implements the Tag Handler object. JSP technology provides multiple Tag Handler interfaces in javax.servlet.jsp.tagext, and TAG, BODYTAG, ITERATIONTAG interfaces are defined in JSP1.2, which adds the SimpleTAG interface in JSP 2.0. JSP also provides the implementation of the above interface Tagsupport, BodyTagsupport, and SimpleTagSupport (SimpleTagsupport only in JSP2.0). Bodytagsupport implements the BodyTag, Tag, and IterationTag interfaces.
Interface and its method
TAG interface
method
SIMPLETAG DOTAG DOSTARTTAG, DOENDTAG, Release Itertag Dostarttag, Doaftertag, Release BodyTAG DOSTARTTAG, DOENDTAG, RELEASE, DOINITBODY, DOAFTERBODY
The table below is from the JSP online tutorial of Sun.
Tag Handler Methods
Tag Handler Type
Methods
SIMPLE
Dostarttag, Doendtag, Release
Attributes
DostartTag, Doendtag, SET / GetAttribute1 ... n, releasebody, evale and no ortho interaction
Dostarttag, Doendtag, Release
Body, ITERATIVE EVALUATION
DostartTag, DoafterBody, Doendtag, Release
Body, Interaction
Dostarttag, Doendtag, Release, DoinitBody, DoafterBody, Release
The EVAL in the table below is an abbreviation of Evaluate, meaning: evaluation, estimation, seeking the value of the ..., the meaning in the following return value is executed.
Return Value Skip_body indicates that the DOENDTAG () method is directly called without the processing tab. Skip_page ignores the JSP content behind the label. After the EVAL_PAGE processes the label, continue to process the contents of the JSP. Eval_body_buffered indicates that the label is required. Eval_body_include indicates that you need to process tabs, but bypass setBodyContent () and DOINITBODY () method EVAL_BODY_AGAIN to loop the label cycle.
For specific usage, you can see other references.
SUN Java Tutorial Related section: http://java.sun.com/webservices/docs/1.0/tutorial/doc/jsptags.html
Second, experiment
1. Test introduction
The following experiment is based on the above development processes.
(1) Specify the URI: <% @ taglib URI = "/ helloworld" prefix = "mytag"%> of the JSP.
(2) Configure Tag-location in Web.xml:
taglib>
(3) Define the processing class that implements the tag in the .tld file specified in tag-location:
tag>
(4) Perform the DostartTAG and DOENDTAG method of processing class mytag.helloworldtag, and then enter the result into the JSP, and output together with the contents in the JSP. In fact, custom labels and other content in JSP are compiled into servlets with WebServer.
2. Directory structure of the completed test
Apply MYJSP under WebApps of Tomcat.
The MyJSP contains J2EE standard directory structure: web-inf and hello.jsp. Web-INF contains subdirectory classes and lib and web.xml, and the TLD file can be placed under Web-INF, or it is placed under the sub-directory of Web-INF.
3. Start experiment
3.1. Write JSP
<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ Taglib Uri = "/ HelloWorld" prefix = "mytag"%>
JSP1
title>
hEAD>
The following shows the content in a custom label
h1>
form>
body>
html>
3.2. Write web.xml
XML Version = "1.0" encoding = "UTF-8"?>
"http://java.sun.com/dtd/web-app_2_3.dtd">
taglib>
web-app>
3.3 Write a TLD file
Xml Version = "1.0" encoding = "ISO-8859-1"?>
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
tag>
taglib>
3.4 Writing a label implementation class
Package mytag;
Import java.io.ioException;
Import javax.servlet.jsp. *; import javax.servlet.jsp.tagext. *;
Public class helloworldtag extends tagsupport {
Public helloworldtag () {
}
Public int dostarttag () THROWS JSptagexception {
RETURN EVAL_BODY_INCLUDE;
}
PUBLIC INT DOENDTAG () THROWS JSptagexception {
Try {
PageContext.getOut (). Write ("Hello World");
}
Catch (IOException EX) {
Throw new JSptagexception ("error");
}
Return Eval_page;
}
}
3.5 Performing effect
To deploy to Tomcat's WebApps directory, start Tomcat, enter: http: // localhost: 8080 / myjsp / hello.jsp
"Hello World" is the result of the processing class output of the label part we define.
3.6 Note:
I did 2 days, always report an error, making it very discouraged, I thought the TLD file or web.xml file configuration was not correct, but I couldn't find why.
Tonight, I finally found the reason, because .class file must be placed in the Classes folder, I put it in the lib.
.jar or servlet files are placed in the lib directory, and .class is in place in the class content, if you want to put it in the lib directory, you must pack the file in MyTag into .jar file, then put myTag.jar in LIB Directory.
I hope you don't make this mistake I have made! ^ _ ^
I have time I will write a note of the Struts detailed processing process.
This note is indeed a custom label of JSP1.2, which has changed in the format of the XML file in JSP2.0, and the other is not known! But I want to change it too much, and the other standard is compatible, interested friends can take a look, this is my study notes, may have some use of others, so it is dedicated!