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:Copy codeThe Code is as follows: package com. fish;
Importjava. io. IOException;
Importjavax. servlet. http. HttpServletRequest;
Importjavax. servlet. jsp. JspException;
Importjavax. servlet. jsp. JspWriter;
Importjavax. servlet. jsp. tagext. TagSupport;
Public class Mytaglib extends TagSupport {
@ Override
Public int doStartTag () throws JspException {
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 new RuntimeException (e); // throw a runtime exception
}
Return super. doStartTag ();
}
}
Step 2:That is, to create a tld file, the tld should also 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:Copy codeThe Code is as follows: <? Xmlversion = "1.0" encoding = "UTF-8"?>
<Taglibxmlns = "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.Copy codeThe Code is as follows: <% @ page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<% @ Tagliburi = "http: // fish/mylibs" prefix = "my" %> // The uri must be the same as the preceding one, 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 + "/";
%>
<! Doctypehtml public "-// W3C // DTDHTML 4.01 Transitional // EN">
<Html>
<Head>
<Basehref = "<% = 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>