A Java code cannot appear because of JSP code specifications. All have the JSP custom tag technology.
Therefore, JSP's custom tag technology is to remove the Java code in JSP. At the same time, encapsulation of the tag's personal feeling is also a manifestation of security, so that no one else knows how to implement internal code.
How can we implement the custom tag technology?
First, you have to build an environment. Apche provides two jar packages: jstl. jar and standar. Jar. import them to the Wed-INF lib directory under the Java Web project. (These two jar packages are available on the Internet and can be directly downloaded from the official website)
The environment has been built. Next I will use a piece of code to create a tag for obtaining the local IP address.
Step 1: Create a Java class that implements the tag interface or directly inherits the tagsupport class and overwrites the dostarttag () method.
Code:PackageCom. fish;
ImportJava. Io. ioexception;
ImportJavax. servlet. http. httpservletrequest;
ImportJavax. servlet. jsp. jspexception;
ImportJavax. servlet. jsp. jspwriter;
ImportJavax. servlet. jsp. tagext. tagsupport;
Public classMytaglibExtendsTagsupport {
@ Override
Public intDostarttag ()ThrowsJspexception {
Httpservletrequestrequest = (httpservletrequest)This. Pagecontext. getrequest (); // you can use pagecontext to obtain eight built-in objects on the server. Request
Jspwriter out =This. Pagecontext. getout (); // you can use pagecontext to obtain eight built-in objects on the server. Out. (pagecontext itself is also a built-in object. Originally, the value 9 is the same as the value 8)
Try{
Out. Print (request. getlocalname (); // obtain the host name.
}Catch(Ioexception e ){
Throw newRuntimeexception (E); // throw a runtime exception
}
Return super. Dostarttag ();
}
}
Step 2: Create a TLD file. First, the TLD file must be created under Web-inf:
Write format we can go to Tomcat to copy, find the webapps folder under the tomcat installation directory, and then enter \ examples \ WEB-INF \ jsp2 contains a TLD file. Copy his header.
The Code is as follows:
<? XML version ="1.0"Encoding =UTF-8"?>
<Taglib xmlns =Http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation =Http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
Version ="2.0">
<Description> A tag library exercising simpletaghandlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-Name> mylibs </short-Name> // write the name casually.
<URI> http: // fish/mylibs </uri> // URI can be written at will, but the following is useful:
<Tag>
<Name> fish </Name> // custom tag name
<Tag-class> com. Fish. mytaglib </Tag-class> // The package name and Class Name of the Java class defined above.
<Body-content> Empty </body-content> // indicates that nothing is written in the tag.
</Tag>
</Taglib>
In fact, each more <tag> </Tag> means one more tag. Therefore, N tags can be written in a TLD.
Step 3: reference in JSP.
<% @ Page Language ="Java"Import ="Java. util .*"Pageencoding ="UTF-8"%>
<% @ Taglib uri ="Http: // fish/mylibs"Prefix ="My"%> // The URI must be the same as the URI above. The following attributes can be written in disorder but will be used below
<%
String Path = request. getcontextpath ();
String basepath = request. getscheme () + "://"
+ Request. getservername () + ":" + request. getserverport ()
+ Path + "/";
%>
<! Doctype HTML public "-// W3C // dtdhtml 4.01 transitional // en">
<HTML>
<Head>
<Base href ="<% = Basepath %>">
<Title> my JSP 'index. jsp 'starting page </title>
</Head>
<Body>
<My: fish/> // here the tag does not match my above. Fish is a tag name defined by you in TLD.
</Body>
</Html>