Because the JSP code specification requires no Java code to appear. All of them have a custom label technology for JSP.
Therefore, the JSP's custom label technology is to remove the JSP in the Java code. At the same time, packaging label personal feeling is also a security embodiment, do not let others know the implementation of internal code.
How does that implement custom label technology?
First you have to build an environment, the Apche company has given two jar packs respectively is jstl. Jars and Standar.jar. Import them under the Wed-inf Lib directory under the Javaweb project. (These two jar packs are many online, can also be downloaded directly from the officer net)
The environment is built. Let me use a piece of code to make a label that gets the local IP.
First step:Create a Java class that implements the tag interface or directly inherits the TagSupport class and overrides the doStartTag () method.
Code:
Copy Code code 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 ()//This through PageContext can get the 8 large built-in objects on the server side. Here request
JspWriter out=this.pagecontext.getout ()////This pagecontext can get 8 large built-in objects on the server side. Here out. (PageContext oneself also belongs to the built-in object, originally 9 big remove one is 8 big)
try {
Out.print (Request.getlocalname ())//This sentence is to obtain the host name
catch (IOException e) {
throw new RuntimeException (e);//Throw a Run-time exception
}
return Super.dostarttag ();
}
}
Step Two:is to create the TLD file, first the TLD will also be built under Web-inf:
Writing format we can go to Tomcat to copy, find the WebApps folder under the Tomcat installation directory, and then enter the \EXAMPLES\WEB-INF\JSP2 inside there is a TLD file. Go and copy his head.
The code is as follows:
Copy Code code 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>//name, whatever.
<uri>http://fish/mylibs</uri>//uri write casually, but the following useful OH
<tag>
<name>fish</name>//Custom Label name
<tag-class>com.fish.mytaglib</tag-class>//is you. The package name plus class name of the Java class defined above
<body-content>empty</body-content>//says nothing is written in the label.
</tag>
</taglib>
In fact, each additional <tag></tag> means more than one label. So a TLD can write n tags inside.
Step Three:Referenced in the JSP.
Copy Code code as follows:
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<% @tagliburi = "Http://fish/mylibs" prefix= "my"%>//this URI should be the same as the above, the following attributes can be written in a random way, but it 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" >
<basehref= "<%=basePath%>" >
<title>my JSP ' index.jsp ' starting page</title>
<body>
<my:fish/>//here This tag is not the same as the one above, Fish is a label name that you define yourself in the TLD.
</body>