jsp Custom Tag Technology of implementation principles and code and platform building steps

  • 2020-06-03 08:04:57
  • OfStack

Because the jsp code specification requires that no sentence of java code appear. So you have jsp's custom tag technology.
So jsp's custom tag technique is to remove the java code from jsp. At the same time, encapsulating labels is also a personal feeling of security, not to let others know the implementation of the internal code.
So how do you implement custom tag technology?

First, you have to build an environment. apche company gave two jar packages, jstl. Jar and standar. Jar. (These two jar packages are widely available online, or can be downloaded directly from the official website)
The environment is built. Now I use 1 piece of code to make a tag to get the local ip.

Step 1: Create an java class that implements the tag interface, or directly inherit from the tagsupport class and override the doStartTag () methods in it.
Code:
 
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 You can get the server side 8 Large built-in objects. Here, request 
JspWriter out=this.pageContext.getOut();//// This through pagecontext You can get the server side 8 Large built-in objects. Here, out.( pageContext Oneself also belong to built-in object, originally 9 Big to remove 1 A is 8 big ) 
try { 
out.print(request.getLocalName());// This is what gets the name of the host  
} catch (IOException e) { 
throw new RuntimeException(e);// throw 1 Three runtime exceptions  
} 
return super.doStartTag(); 
} 
} 

Step 2: Create the tld file. First, create tld under ES35en-ES36en:
We can copy the writing format in tomcat, find the webapps folder under the tomcat installation directory, and then enter \examples\ ES42en-ES43en \jsp2 where there is an tld file. Go copy his head.
The 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>// Just write your name  
<uri>http://fish/mylibs</uri>//uri Feel free to write, but the following will help  
<tag> 
<name>fish</name>// Custom tag names  
<tag-class>com.fish.Mytaglib</tag-class>// That's what you defined above java The package name plus the class name  
<body-content>empty</body-content>// That means there's nothing in the tag  
</tag> 
</taglib> 

Actually every time you add 1 more < tag > < /tag > That means one more tag. So I can write n tags in 1 tld.
Step 3: Reference in jsp.
 
<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
<%@tagliburi="http://fish/mylibs"prefix="my"%>// Inside this uri With the above 1 Well, I can scribble the attributes down here but I'll use them down here  
<% 
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/>// This label here is not the same as the one above my There you go, fish Is in the tld It's your own definition 1 A tag name.  
</body> 
</html> 

Related articles: