自定义标签

JSP自定义标签用于减少JSP的java代码

实现Tag接口

package com.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class Tag1 implements Tag{
    private PageContext pc;

    @Override
    public int doEndTag() throws JspException {
        System.out.println("doEndTag");
        return 0;
    }

    @Override
    public int doStartTag() throws JspException {
        System.out.println("doStartTag");
        HttpServletRequest request = (HttpServletRequest) pc.getRequest();
        JspWriter out = pc.getOut();
        String ip = request.getRemoteAddr();
        System.out.println(ip);
        try {
            out.write(ip);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Tag getParent() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void release() {
        System.out.println("release");
    }

    @Override
    public void setPageContext(PageContext pc) {
        System.out.println("setPageContext");
        this.pc = pc;
    }

    @Override
    public void setParent(Tag t) {
        // TODO Auto-generated method stub

    }

}

tld文件

这里写图片描述

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
     http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <!-- description用来添加对taglib(标签库)的描述 -->
    <description>自定义标签库</description>
    <!--taglib(标签库)的版本号 -->
    <tlib-version>1.0</tlib-version>
    <short-name>FSLTagLibrary</short-name>
    <!-- 
        为自定义标签库设置一个uri,uri以/开头,/后面的内容随便写,如这里的/FSL ,
        在Jsp页面中引用标签库时,需要通过uri找到标签库
        在Jsp页面中就要这样引入标签库:<%@taglib uri="/FSL" prefix="FSL"%>
    -->
    <uri>/FSL</uri>

    <!--一个taglib(标签库)中包含多个自定义标签,每一个自定义标签使用一个tag标记来描述  -->
    <!-- 一个tag标记对应一个自定义标签 -->
     <tag>
        <description>print out client's IP address</description>
        <!-- 
            为标签处理器类配一个标签名,在Jsp页面中使用标签时是通过标签名来找到要调用的标签处理器类的
            通过outIP就能找到对应的com.tag.Tag1类
         -->
        <name>outIP</name>
        <!-- 标签对应的处理器类-->
        <tag-class>com.tag.Tag1</tag-class>
        <body-content>empty</body-content>
    </tag>

</taglib>

JSP引用

<%@ taglib prefix="FSL" uri="/FSL" %>
...
<FSL:outIP/>

后台打印结果

setPageContext
doStartTag
127.0.0.1
doEndTag

页面打印
这里写图片描述

执行过程

1.servlet的Init()实例化标签处理池

_005fjspx_005ftagPool_005fFSL_005foutIP_005fnobody = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(getServletConfig());

2.通过标签处理池实例化自定义标签处理类com.tag.Tag1

 com.tag.Tag1 _jspx_th_FSL_005foutIP_005f0 = (com.tag.Tag1) _005fjspx_005ftagPool_005fFSL_005foutIP_005fnobody.get(com.tag.Tag1.class);

3.然后是处理方法setPageContext->setParent->doStartTag->doEndTag->reuse

setPageContext将JSP的pageContext对象传递给自定义标签处理类
setParent将当前标签的父标签(tag t)传给处理类,没有则传null

    _jspx_th_FSL_005foutIP_005f0.setPageContext(_jspx_page_context);
    _jspx_th_FSL_005foutIP_005f0.setParent(null);
    int _jspx_eval_FSL_005foutIP_005f0 = _jspx_th_FSL_005foutIP_005f0.doStartTag();
    if (_jspx_th_FSL_005foutIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) {
      _005fjspx_005ftagPool_005fFSL_005foutIP_005fnobody.reuse(_jspx_th_FSL_005foutIP_005f0);
      return true;
    }
    _005fjspx_005ftagPool_005fFSL_005foutIP_005fnobody.reuse(_jspx_th_FSL_005foutIP_005f0);
    return false;
  }

4.servlet调用destroy方法时会调用自定义标签处理类的release()

    _005fjspx_005ftagPool_005fFSL_005foutIP_005fnobody.release();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值