Test. jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
To change test. jsp to the custom tag method, we use two different methods: simple tag and content tag.
1. Simple tag
Because we need to output two contents (date and file name), we create a parameter for the tag. Code:
DemoTag.java package com.mycompany;import java.util.Date;import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class DemoTag extends TagSupport { public int doStartTag() throws JspException { try { HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); JspWriter out = pageContext.getOut(); if (parameter.compareToIgnoreCase("filename") == 0) out.print(request.getServletPath()); else out.print(new Date()); } catch (java.io.IOException e) { throw new JspTagException(e.getMessage()); } return SKIP_BODY; } private String parameter = "date"; public void setParameter(String parameter) { this.parameter = parameter; } public String getParameter() { return parameter; }}
Next, we will create the Tag file mytaglib. TLD. The tag file is actually a description file in XML format, and the content is also very simple.
Mytaglib. TLD
<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><tag> <name>demo</name> <tag-class>com.mycompany.DemoTag</tag-class> <body-content>empty</body-content> <attribute> <name>parameter</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute></tag></taglib>
In this Tag file, we name the created tag named demo and declare the type and parameter (parameter ). Save the file under/WEB-INF.
Of course, we also need to add our custom tags to Web. XML, otherwise they will still be unavailable.
Added in Web. xml:
<jsp-config> <taglib> <taglib-uri>/WEB-INF/MyTagLib</taglib-uri> <taglib-location>/WEB-INF/MyTagLib.tld</taglib-location> </taglib> </jsp-config>
You may have seen similar declarations elsewhere, but there is no external JSP-config, but we use DTD 2.4. If not, eclipse will prompt an error.
So far, our custom tags have been created. Next, we can start to rewrite the JSP file to separate the code.
Test. jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/WEB-INF/MyTagLib" prefix="mytag"%>
You must be familiar with the above, so I will not say much.
2. Content tag
The creation process is almost the same as above, but there are some differences between program files and configuration content.
Demotag2.java package com. mycompany;
import java.io.IOException;import java.util.Date;import javax.servlet.http.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class DemoTag2 extends BodyTagSupport { public int doStartTag() throws JspTagException { return EVAL_BODY_BUFFERED; } public int doEndTag() throws JspTagException { String body = this.getBodyContent().getString(); HttpServletRequest request = (HttpServletRequest)pageContext.getRequest(); body = body.replace("$date", new Date().toString()); body = body.replace("$filename", request.getServletPath()); try { pageContext.getOut().print(body); } catch (IOException e) { throw new JspTagException(e.getMessage()); } return SKIP_BODY; }}
Add the new tag demotag2 to the Tag file above.
Mytaglib. TLD
<?xml version="1.0" encoding="ISO-8859-1"?><taglib><tlib-version>1.0</tlib-version><jsp-version>1.2</jsp-version><tag> <name>demo</name> <tag-class>com.mycompany.DemoTag</tag-class> <body-content>empty</body-content> <attribute> <name>parameter</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute></tag><tag> <name>demo2</name> <tag-class>com.mycompany.DemoTag2</tag-class> <body-content>jsp</body-content></tag></taglib>
The Web. xml file does not need to be modified.
Check the test. jsp effect of both labels.
Test. jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@taglib uri="/WEB-INF/MyTagLib" prefix="mytag"%>
Now, both the label methods are complete.
This article does not describe related technical details. We recommend that you refer to Sun's official documents on JSP custom tags.