JSP custom tag case analysis and jsp custom Case Analysis
This article introduces the use cases of JSP custom tags for your reference. The specific content is as follows:
Case 1: implement a basic anti-leech label
1. Tag Processing
Public class MyReferer extends BodyTagSupport {private String site; private String back; public String getSite () {return site;} public void setSite (String site) {this. site = site;} public String getBack () {return back;} public void setBack (String back) {this. back = back;} public int doEndTag () throws JspException {// obtain the JSP context object PageContext pageContext = this. pageContext; // get the request object HttpServletRequest request = (HttpServletRequest) pageContext. getRequest (); // judge String header = request. getHeader ("referer"); if (header! = Null & header. startsWith (getSite () {// execute the subsequent page return Tag. EVAL_PAGE;} else {// page redirection HttpServletResponse response = (HttpServletResponse) pageContext. getResponse (); try {response. sendRedirect (getBack ();} catch (IOException e) {e. printStackTrace ();} // do not execute return Tag. SKIP_PAGE ;}}}
2. description file
<? Xml version = "1.0" encoding = "UTF-8"?> <Taglib xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version = "2.1"> <! -- 2. compile the tag library description file --> <tlib-version> 1.0 </tlib-version> <short-name> jnb </short-name> <tag> <name> referer </ name> <tag-class> cn. itcast. custom. myReferer </tag-class> <body-content> empty </body-content> <attribute> <name> site </name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> <name> back </name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> </tag> </taglib>
3. Introduction and use
<%@taglib uri="/WEB-INF/referer.tld" prefix="my"%> <my:referer site=http://localhost:8080/day11/list.jsp back="/day11/list.jsp"/>
JSP2.0 custom tag
--- | SimpleTag Interface
Defines the lifecycle method of the tag processing class. DoTag ()
----- | SimpleTagSupport class
All implement SimpleTag interface methods, so we only need to inherit and override this class.
Case 2: implement your own if .... Else tag
Objectives:
<% @ Taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <c: choose> <c: when test = "<% = 12> 1%>"> greater than </c: when> <c: otherwise> less than </c: otherwise> </c: choose>
Analysis:
1. ChooseTag. java, a tag field attribute must be defined
Public class ChooseTag extends SimpleTagSupport {private boolean tag = true; public boolean isTag () {return tag;} public void setTag (boolean tag) {this. tag = tag;} // automatically execute public void doTag () throws JspException, IOException {// get the tag body object JspFragment body = this. getJspBody (); // execute the label body. invoke (null); super. doTag ();}}
2. WhenTag. java
Public class WhenTag extends SimpleTagSupport {private boolean test; public boolean isTest () {return test;} public void setTest (boolean test) {this. test = test;} // automatically execute public void doTag () throws JspException, IOException {// obtain the parent element ChooseTag choose = (ChooseTag) this. getParent (); // obtain the flag variable value of the parent element boolean parent = choose. isTag (); // judge if (parent & this. isTest () {// execute the label body JspFragment body = this. getJspBody (); body. invoke (null);} super. doTag ();}}
3. Otherwise. java
Public class OtherwiseTag extends SimpleTagSupport {// automatically execute public void doTag () throws JspException, IOException {// obtain the parent element ChooseTag choose = (ChooseTag) this in case of a tag. getParent (); // obtain the flag variable value of the parent element boolean parent = choose. isTag (); // judge if (parent) {// execute the label body JspFragment body = this. getJspBody (); body. invoke (null);} super. doTag ();}}
4. description file
<? Xml version = "1.0" encoding = "UTF-8"?> <Taglib xmlns =" http://java.sun.com/xml/ns/javaee "Xmlns: xsi =" http://www.w3.org/2001/XMLSchema-instance "Xsi: schemaLocation =" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee /Web-jsptaglibrary_2_1.xsd "version =" 2.1 "> <! -- 2. compile the tag library description file --> <tlib-version> 1.0 </tlib-version> <short-name> jnb </short-name> <tag> <name> choose </ name> <tag-class> cn. itcast. tags. chooseTag </tag-class> <body-content> scriptless </body-content> using JSP2.0 </tag> <name> when </name> <tag- class> cn. itcast. tags. whenTag </tag-class> <body-content> scriptless </body-content> <attribute> <name> test </name> <required> true </required> <rtexprvalue> true </rtexprvalue> </attribute> </tag> <name> otherwise </name> <tag-class> cn. itcast. tags. otherwiseTag </tag-class> <body-content> scriptless </body-content> </tag> </taglib>
5. Introduction and use
<% @ Taglib uri = "/WEB-INF/ifelse. tld "prefix =" jnb "%> <jnb: choose> <jnb: when test =" <% = 1> 2%> "> less than </jnb: when> <jnb: otherwise> greater than </jnb: otherwise> </jnb: choose>
Package custom tag Library
1. Create a taglibs folder
2. Copy the class files corresponding to all tag processing classes together with the package to the directory in 1.
3. Create a META-INF folder in the folder in 1
4. Copy the tld file to the META-INF directory
5. Edit the tld file to introduce the uri element: <uri> http://www.jnb.com </uri> à provide the introduced url path
6. Use the jar command to package: D: \ mytaglibs> jar cvf jnb. jar *
Summary
Master how to use JSP2.0 to develop and package custom labels.
1. Create a taglibs folder
2. Copy the class files corresponding to all tag processing classes together with the package to the directory in 1.
3. Create a META-INF folder in the folder in 1
4. Copy the tld file to the META-INF directory
5. Edit the tld file to introduce the uri element: <uri> http://www.jnb.com </uri> à provide the introduced url path
6. Use the jar command to package: D: \ mytaglibs> jar cvf jnb. jar *
Summary
Master how to use JSP2.0 to develop and package custom labels.
Articles you may be interested in:
- JSP custom tag
- A simple example of a JSP custom tag
- Jsp custom tag instance
- Summary of the implementation process of JSP custom tag Taglib
- Introduction to using instances Based on JSP custom tags
- Jsp custom tag Technology (implementation principles, code, and platform building steps)
- How to obtain user IP addresses using JSP custom tags
- ForEach traversal and escape character examples of simple custom jsp labels
- Examples of JSP custom tag rtexprvalue attribute usage