一、开发防盗链标签
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/example" prefix="example" %>
<example:referer site="http://localhost" page="index.jsp"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>防盗链</title>
</head>
<body>
内容 <br>
</body>
</html>
标签处理器类:
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagDemo1 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();
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
//1.得到来访者的referer
String referer = request.getHeader("referer");
//判断来访者的页面是不是防盗链的网站
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();
}
}
}
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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>example</short-name>
<uri>/example</uri>
<tag>
<name>referer</name>
<tag-class>come.guigu.web.simpletag.example.TagDemo1</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>
</taglib>
二、开发<c:if>标签
jsp页面
<%@ 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>My JSP '2.jsp' starting page</title>
</head>
<body>
<%
session.setAttribute("user", "aaa");
%>
<c:if flag="${user==null }">
没登录<br>
</c:if>
<c:if flag="${user!=null }">
登录<br>
</c:if>
</body>
</html>
标签处理器类:
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagDemo2 extends SimpleTagSupport {
private boolean flag;
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void doTag() throws JspException, IOException {
if(flag) {
this.getJspBody().invoke(null);
}
}
}
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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>example</short-name>
<uri>/example</uri>
<tag>
<name>if</name>
<tag-class>come.guigu.web.simpletag.example.TagDemo2</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>flag</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
三、开发<c:if><c:else>标签
jsp页面
<%@ 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>My JSP '3.jsp' starting page</title>
</head>
<body>
<%
session.setAttribute("user", "aaa");
%>
<c:choose>
<c:when flag="${user!=null }">
登录 <br>
</c:when>
<c:otherwise>
没登录<br/>
</c:otherwise>
</c:choose>
</body>
</html>
标签处理器类:
父类:
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagDemo3 extends SimpleTagSupport {
private boolean isdo;
public boolean isIsdo() { //get
return isdo;
}
public void setIsdo(boolean isdo) { //set
this.isdo = isdo;
}
@Override
public void doTag() throws JspException, IOException {
this.getJspBody().invoke(null);
}
}
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagDemo3_1 extends SimpleTagSupport {
private boolean flag;
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void doTag() throws JspException, IOException {
TagDemo3 parent = (TagDemo3) this.getParent();
if(flag && !parent.isIsdo()) {
this.getJspBody().invoke(null); //执行标签体
parent.setIsdo(true);
}
//条件不成立,什么事都不干
}
}
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class TagDemo3_2 extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
TagDemo3 parent = (TagDemo3) this.getParent();
if(!parent.isIsdo()) {
this.getJspBody().invoke(null);
parent.setIsdo(true);
}
}
}
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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>example</short-name>
<uri>/example</uri>
<tag>
<name>choose</name>
<tag-class>come.guigu.web.simpletag.example.TagDemo3</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>come.guigu.web.simpletag.example.TagDemo3_1</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>flag</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>come.guigu.web.simpletag.example.TagDemo3_2</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>