JavaWeb学习记录(十九)——开发JSTL自定义标签

一、防盗链标签

import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class RefererTag extends SimpleTagSupport{
    private String name;
    private String page;
    public void setName(String name) {
        this.name = name;
    }
    public void setPage(String page) {
        this.page = page;
    }
    
    @Override
    public void doTag() throws JspException, IOException {
        PageContext pageContext=(PageContext) this.getJspContext();
        HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
        String referer=request.getHeader("Referer");
        JspFragment jf=this.getJspBody();
        System.out.println(name);
        System.out.println(referer);
        if(referer==null||!referer.startsWith(name)){
            HttpServletResponse response=(HttpServletResponse) pageContext.getResponse();
            response.sendRedirect("page");
        }else{
            
        }
    }
}

 <tag>
        <description>description</description>
        <name>referer</name>
        <tag-class>web17.tag.RefererTag</tag-class>
        <body-content>scriptless</body-content>
        <!-- 设置属性 -->
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>page</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

  测试

  <h1>首页</h1>
      <a href="./test.jsp">测试</a>

二、if标签

  package web17.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class IfTag extends SimpleTagSupport{
    private boolean test;
    
    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf=this.getJspBody();
        if(test){
            jf.invoke(null);
        }else{
            
        }
    }
}

  <tag>
        <description>description</description>
        <name>if</name>
        <tag-class>web17.tag.IfTag</tag-class>
        <body-content>scriptless</body-content>
        <!-- 设置属性 -->
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

  测试

  <c:if test="${1==1 }">1==1为真执行</c:if>
      <c:if test="${1>=2 }">1>=2为真执行</c:if>

三、选择(if-else)标签

  package web17.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ChooseTag extends SimpleTagSupport{
    private boolean isOk;
    
    public boolean isOk(){
        return isOk;
    }
    public void setOk(boolean isOk){
        this.isOk=isOk;
    }
    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf=this.getJspBody();
        jf.invoke(null);
    }
}
  package web17.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class WhenTag extends SimpleTagSupport{
    private boolean test;
    
    public void setTest(boolean test) {
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf=this.getJspBody();
        if(test){
            jf.invoke(null);
        }else{
            
        }
    }
}

  package web17.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class OtherWiseTag extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException {
        ChooseTag ct=(ChooseTag) this.getParent();
        JspFragment jf=this.getJspBody();
        if(ct.isOk()){
            jf.invoke(null);
        }else{
            
        }
    }
}

  <tag>
        <description>description</description>
        <name>choose</name>
        <tag-class>web17.tag.ChooseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>
     <tag>
        <description>description</description>
        <name>when</name>
        <tag-class>web17.tag.WhenTag</tag-class>
        <body-content>scriptless</body-content>
        <!-- 设置属性 -->
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <description>description</description>
        <name>otherwise</name>
        <tag-class>web17.tag.OtherWiseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

  测试

  <h1>选择执行</h1>
      <c:choose>
          <c:when test="${1==1 }">1==1</c:when>
          <c:otherwise>1!=1</c:otherwise>
      </c:choose>

四、迭代标签

  package web17.tag;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class ForEachTag extends SimpleTagSupport{
    private String var;
    private Object items;
    
    public void setVar(String var) {
        this.var = var;
    }

    public void setItems(Object items) {
        this.items = items;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspFragment jf=this.getJspBody();
        if(items!=null){
            if(items instanceof List){
                List item=(List)items;
                for(int i=0;i<item.size();i++){
                    Object v=item.get(i);
                    this.getJspContext().setAttribute(var, v);
                    jf.invoke(null);
                }
            }else if(items instanceof Set){
                Set set=(Set)items;
                Iterator<Set> it=set.iterator();
                while(it.hasNext()){
                    Object v=it.next();
                    this.getJspContext().setAttribute(var, v);
                    jf.invoke(null);
                }
            }else if(items instanceof Map){
                Map map=(Map)items;
                Set<Map.Entry> set=map.entrySet();
                Iterator<Entry> it=set.iterator();
                while(it.hasNext()){
                    Entry entry=it.next();
                    this.getJspContext().setAttribute(var, entry);
                    jf.invoke(null);
                }
            }else if(items instanceof int[]){
                int arr[]=(int[]) items;
                for(int i=0;i<arr.length;i++){
                    this.getJspContext().setAttribute(var, arr[i]);
                    jf.invoke(null);
                }
            }
        }
    }
}

   <tag>
        <description>description</description>
        <name>forEach</name>
        <tag-class>web17.tag.ForEachTag</tag-class>
        <body-content>scriptless</body-content>
        <!-- 设置属性 -->
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <!-- 设置属性 -->
        <attribute>
            <name>items</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

  测试

  <%
          List<String> list=new ArrayList<String>();
          list.add("zsf1");
          list.add("zsf2");
          list.add("zsf3");
          pageContext.setAttribute("list", list);
       %>
       <br/>
       <c:forEach items="${list}" var="x">
           ${x }
       </c:forEach>
      
      
       <%
          Set<String> set=new HashSet<String>();
          set.add("zsf11");
          set.add("zsf22");
          set.add("zsf33");
          pageContext.setAttribute("set", set);
       %>
       <br/>
       <c:forEach items="${set}" var="x">
           ${x }
       </c:forEach>
      
      
       <%
          Map<String,String> map=new HashMap<String,String>();
          map.put("name1","zsf111");
          map.put("name2","zsf222");
          map.put("name3","zsf333");
          pageContext.setAttribute("map", map);
       %>
       <br/>
       <c:forEach items="${map}" var="x">
           ${x.key }
           ${x.value }
       </c:forEach>
       <br/>
       <%
           int[] arr={1,2,3,4};
           pageContext.setAttribute("arr", arr);
        %>
        <c:forEach items="${arr }" var="r">${r }</c:forEach>
       

五、html转义标签

  package web17.util;

public class HtmlFilter {
    public static String htmlFilter(String content) {
        //定义字符数组
        char chars[]=new char[content.length()];
        //把字符串直接复制到chars[]数组中
        content.getChars(0, content.length(), chars, 0);
        StringBuffer sb=new StringBuffer();
        for(int index=0;index<chars.length;index++){
            char c=chars[index];
            switch (c) {
            case '>':
                sb.append("&gt;");
                break;
            case '<':
                sb.append("&lt;");
                break;
            default:
                sb.append(c);
                break;
            }
        }
        return sb.toString();
    }
}

  package web17.tag;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import web17.util.HtmlFilter;

public class EscapeTag extends SimpleTagSupport {
    @Override
    public void doTag() throws JspException, IOException {
        //获取操作对象
        JspFragment jf=this.getJspBody();
        //写到流中
        StringWriter sw=new StringWriter();
        //写入该流
        jf.invoke(sw);
        //获取这个内容
        String content=sw.getBuffer().toString();
        String sb = HtmlFilter.htmlFilter(content);
        //输出
        JspWriter out=this.getJspContext().getOut();
        //写出
        out.println(sb);
    }

    
}

  <tag>
        <description>description</description>
        <name>escape</name>
        <tag-class>web17.tag.EscapeTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

  测试

  <c:escape>
            <a href="">点点</a>
        </c:escape>

原文地址:https://www.cnblogs.com/ly-radiata/p/4372445.html