一、防盗链标签

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>

JavaWeb学习记录(十九)——开发JSTL自定义标签的更多相关文章

  1. JavaWeb学习记录(十九)——jstl自定义标签库之传统标签

    一.传统标签 (1)JSP引擎将遇到自定义标签时,首先创建标签处理器类的实例对象,然后按照JSP规范定义的通信规则依次调用它的方法. public void setPageContext(PageCo ...

  2. JavaWeb学习记录(十九)——jstl自定义标签之简单标签

    一.简单标签共定义了5个方法: setJspContext方法 setParent和getParent方法 setJspBody方法 doTag方法 二.方法介绍 osetJspContext方法 用 ...

  3. JavaWeb学习笔记(九)—— JSTL标签库

    一.JSTL概述 1.1 什么是JSTL  JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一样,只不过它不是 ...

  4. javaweb学习总结(十九)——JSP标签

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...

  5. JavaWeb学习记录(九)——Cookie的增加、删除、查看

    一.servlet功能代码: public void doGet(HttpServletRequest request, HttpServletResponse response)           ...

  6. javaweb学习总结十九(http协议概述以及http请求信息分析)

    一:http协议概述 1:http协议定义 2:使用telnet程序连接web服务器,获取资源 3:http协议:超文本传输协议,它是基于tcp/ip协议,就是建立在tcp/ip之上工作的, http ...

  7. Bootstrap3.0学习第十九轮(JavaScript插件——标签页)

    详情请看 http://aehyok.com/Blog/Detail/46.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  8. 学习笔记:CentOS7学习之十九:Linux网络管理技术

    目录 学习笔记:CentOS7学习之十九:Linux网络管理技术 本文用于记录学习体会.心得,兼做笔记使用,方便以后复习总结.内容基本完全参考学神教育教材,图片大多取材自学神教育资料,在此非常感谢MK ...

  9. Lua和C++交互 学习记录之九:在Lua中以面向对象的方式使用C++注册的类

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 在 ...

随机推荐

  1. K2与OData和Swagger集成,从任何设备无需代码获取数据

    K2近期宣布获得了DData和Swagger REST的支持,这件事情究竟有多好呢? K2与OData和Swagger的集成,保障K2 Blackpearl的用户能建立基于工作流和表单的解决方案,最重 ...

  2. (转载)java常见的ClassNotFoundException

    1 - java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory   添加包common-logging.jar2 ...

  3. 黑马程序员——OC语言Foundation框架 结构体

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)结构体 NSRange(location length) NSPoi ...

  4. Allow windows service to "Interact with desktop"

    Typically, services are designed to run unattended without any UI with any need to interact with des ...

  5. hdu 2082

    ps:get到了母函数...看了好久的模板与介绍....似懂非懂..决定要多找些题来试试... 代码: #include "stdio.h" #include "stri ...

  6. PHP安装编译配置参考

    编辑安装php的参考配置: ./configure --prefix=/usr/local/php-5.6.8 --with-config-file-path=/usr/local/php-5.6.8 ...

  7. iOS 在UILabel显示不同的字体和颜色

    转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...

  8. Object-C 基础笔记1--杂识

    一,常量 1, 常量字符串永远不会release; 2,使用常量字符串初始化另一个字符串,这两个字符串相等. 3,相同内容的常量地址值相同. 二, @class 声明一个类,一般是在.h文件中使用,不 ...

  9. C#指针转换

    下表显示了预定义的隐式指针转换. 隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中. 隐式指针转换       From To 任何指针类型 void* null 任何指针类型 显式指针转换 ...

  10. C中的setjmp与longjmp

    setjmp与longjmp是属于C语言中的,当然,C++也会有这两个函数了.他们的原型如下: int setjmp( jmp_buf env ); 作用:第一次调佣时,将寄存器的当前状态信息全部存入 ...