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.
package com.fish; 
public class Mytaglib extends TagSupport{ 
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:
<description>A tag library exercising SimpleTaghandlers.</description> 
<short-name>mylibs</short-name>// Just write your name  
<uri>http://fish/mylibs</uri>//uri Feel free to write, but the following will help  
<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  

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"> 
<title>My JSP 'index.jsp' starting page</title> 
<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.  

Related articles: