标签:
<?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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>SimpleTagLibrary</short-name>
<uri>/lcp</uri>
<tag>
<name>referer</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>if</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>choose</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.OtherwiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>foreach</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>foreach2</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.ForEachTag2</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>htmlFilter</name> <!-- 为标签处理器类配一个标签名 -->
<tag-class>cn.lcp.web.tag.HtmlFilter</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘index.jsp‘ starting page</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/1.jsp">ahahah</a>
</body>
</html>
1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/lcp" prefix="lcp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<itcast:referer site="http://localhost:8080/" page="/index.jsp"/>
<html>
<head>
<title>防盗链</title>
</head>
<body>
ahhah
</body>
</html>
RefererTag.java
public class RefererTag extends SimpleTagSupport {
private String site;
private String page;
public void setSite(String site) {
this.site = site;
}
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"); //http://www.sina.com/index.html
//判断
if(referer==null || !referer.startsWith(site)){
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
String webroot = request.getContextPath();
if(page.startsWith(webroot)){
response.sendRedirect(page);
}else{
response.sendRedirect(webroot + page);
}
//重定向后,控制保护的页面不要执行
throw new SkipPageException();
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/example" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>c:if标签,是否执行标签体</title>
</head>
<body>
<c:if test="${user==null }">
xxxx
</c:if>
<c:if test="${user !=null }">
xxxx
</c:if>
</body>
</html>
package cnl.lcp.web.tag.example;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//if标签
public class IfTag extends SimpleTagSupport {
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test){
this.getJspBody().invoke(null);
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/example" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>if...else标签</title>
</head>
<body>
<c:choose>
<c:when test="${user!=null}">
xxxx
</c:when>
<c:otherwise>
yyyy
</c:otherwise>
</c:choose>
</body>
</html>
package cnl.lcp.web.tag.example;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class OtherwiseTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
//拿到父标签
ChooseTag parent = (ChooseTag) this.getParent();
if(parent.isOk()==false){
this.getJspBody().invoke(null);
parent.setOk(true);
}
}
}
package cnl.lcp.web.tag.example;
import java.io.IOException;
import javax.servlet.jsp.JspException;
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 {
//得到父标签,已经通过setParent吧父标签传递进来了。
ChooseTag parent = (ChooseTag) this.getParent();
//test并且父标签没有执行过,
if(test==true && parent.isOk()==false){
this.getJspBody().invoke(null); //执行标签体
parent.setOk(true); //控制兄弟不要执行
}
}
}
package cnl.lcp.web.tag.example;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ChooseTag extends SimpleTagSupport {
private boolean isOk;
public boolean isOk() { //相当于get方法
return isOk;
}
public void setOk(boolean isOk) { //相当于set方法
this.isOk = isOk;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null); //控制标签体执行
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/example" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>foreach标签,重复执行标签体</title>
</head>
<body>
<!-- 迭代list集合 -->
<%
List list = new ArrayList();
list.add("a");
list.add("b");
request.setAttribute("list",list);
%>
<c:foreach var="str" items="${list}">
${str }
</c:foreach>
<br/><hr><br/><br/>
<!-- 迭代map集合 -->
<%
Map map = new HashMap();
map.put("a","aaaa");
map.put("b","bbbb");
request.setAttribute("map",map);
%>
<c:foreach2 items="${map}" var="entry">
${entry.key }=${entry.value }<br/>
</c:foreach2>
<br/><hr><br/><br/>
<!-- 迭代数组 -->
<%
Integer num[] = {1, 2, 3, 4};
request.setAttribute("num", num);
%>
<c:foreach2 items="${num}" var="i">
${i }
</c:foreach2>
<!-- 迭代数组 -->
<%
int num1[] = {1, 2, 3, 4};
request.setAttribute("num", num1);
%>
<c:foreach2 items="${num}" var="i">
${i }
</c:foreach2>
</body>
</html>
package cnl.lcp.web.tag.example;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForEachTag extends SimpleTagSupport {
private List items;
private String var;
public void setItems(List items) {
this.items = items;
}
public void setVar(String var) {
this.var = var;
}
@Override
//迭代集合
public void doTag() throws JspException, IOException {
PageContext context = (PageContext) this.getJspContext();
Iterator it = items.iterator();
while(it.hasNext()){
Object obj = it.next();
//
context.setAttribute(var, obj);
this.getJspBody().invoke(null);
}
}
}
package cnl.lcp.web.tag.example;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForEachTag2 extends SimpleTagSupport {
private Object items;
private String var;
private Collection collection; //记住集合类型
public void setItems(Object items) { //int[]
this.items = items;
if(items instanceof Collection){ //判断是否是单链集合
collection = (Collection) items; //list set
}
if(items instanceof Map){ //如果是双链集合,先强转为map,再转成单链集合
Map map = (Map) items;
collection = map.entrySet(); //set
}
//对任意数组进行操作。
if(items.getClass().isArray()){
this.collection = new ArrayList();
int len = Array.getLength(items);
for(int i = 0; i < len; i ++){
Object obj = Array.get(items, i);
this.collection.add(obj);
}
}
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext = (PageContext) this.getJspContext();
Iterator it = collection.iterator(); //null.iterator
while(it.hasNext()){
Object obj = it.next(); //
pageContext.setAttribute(var, obj);
this.getJspBody().invoke(null);
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/example" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>html转移标签,修改标签体</title>
</head>
<body>
<a href="">点点</a>
<!-- 对标签内容进行转义 -->
<c:htmlFilter>
<a href="">点点</a>
</c:htmlFilter>
</body>
</html>
package cnl.lcp.web.tag.example;
import java.io.IOException;
import java.io.StringWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HtmlFilter extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
//invoke写到容器里面需要缓冲流
StringWriter sw = new StringWriter();
this.getJspBody().invoke(sw); //得到标签体,再把标签体写到一个容器里面去。
//得到标签体的内容
String content = sw.getBuffer().toString();
content = filter(content);
this.getJspContext().getOut().write(content);
}
//转义字符串,是Apache里面的代码
private String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case ‘<‘:
result.append("<");
break;
case ‘>‘:
result.append(">");
break;
case ‘&‘:
result.append("&");
break;
case ‘"‘:
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
标签:
原文地址:http://www.cnblogs.com/lcpholdon/p/4380668.html