案例一:防盗链标签
public class RefererTag extends SimpleTagSupport {
//site属性为自己的地址
private String site;
//page为对盗链者跳转的页面
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 {
//1.得到来访问者referer
PageContext pageContext = (PageContext) this.getJspContext();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
String referer = request.getHeader("referer");
//2.判断来访者的页面是不是要防盗链的网站
if(referer==null || !referer.startsWith(site)){
if(page.startsWith(request.getContextPath())){
response.sendRedirect(page);
}else if(page.startsWith("/")){
response.sendRedirect(request.getContextPath() + page);
}else{
response.sendRedirect(request.getContextPath() + "/" + page);
}
throw new SkipPageException();
}
}
}
案例二:模拟sun公司的<c:when><c:otherwise>标签
jsp中
<c:choose>
<c:when test="${user==null }">
aaaa
</c:when>
<c:otherwise>
bbbb
</c:otherwise>
</c:choose>
//ChooseTag
public class ChooseTag extends SimpleTagSupport {
//定义标记
private boolean isDo;
public boolean isDo() { //get
return isDo;
}
public void setDo(boolean isDo) { //set
this.isDo = isDo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
//WhenTag
public class WhenTag extends SimpleTagSupport {
private boolean test;
public void setTest(boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
//得到父标签
ChooseTag parent = (ChooseTag) this.getParent();
//判断test为true,并且父标签为真,表示没有执行过
if(test && !parent.isDo()){
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
//OtherWiseTag
public class OtherWiseTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
ChooseTag parent = (ChooseTag) this.getParent();
if(!parent.isDo()){
this.getJspBody().invoke(null);
parent.setDo(true);
}
}
}
案例三:迭代集合标签(模拟<c:foreach>)
JSP中
<c:foreach2 var="str" items="${list }">
${str }
</c:foreach2>
public class ForeachTag extends SimpleTagSupport {
//war为在域中存储的关键字
private String var;
//items为要迭代的集合或数组
private Object items;
//无论items是什么,都转成单列集合collection
private Collection collection;
public void setItems(Object items) { //int[]
this.items = items; //int[]
if(items instanceof Collection){ //list set
collection = (Collection) items;
}
//是map就转成set
if(items instanceof Map){
Map map = (Map) items;
collection = map.entrySet(); //set
}
//Class类中有个isArray方法判断是否是数组。
if(items.getClass().isArray()){
//建立临时容器
this.collection = new ArrayList();
//反射包中有个操作数组的类Array,得到数组的长度
int length = Array.getLength(items);
for(int i=0;i<length;i++){
//获取数组中的元素
Object value = Array.get(items, i);
//添加到临时容器中去
this.collection.add(value);
}
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
Iterator it = this.collection.iterator();
while(it.hasNext()){
Object value = it.next();
//以var为关键字存入域中,通知浏览器取走。
this.getJspContext().setAttribute(var, value);
this.getJspBody().invoke(null);
}
}
}
案例四:HTML中的转义标签
public class HtmlFilterTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
StringWriter sw = new StringWriter();
JspFragment jf = this.getJspBody();
//写到带缓冲的流中,用于取出修改
jf.invoke(sw);
//如:<a href="">点点</a>
String content = sw.getBuffer().toString();
content = filter(content);
this.getJspContext().getOut().write(content);
}
//此方法在C:\apache-tomcat-6.0.36\webapps\examples\WEB-INF\classes\util\HTMLFilter.java中抄的
public 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());
}
}