<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/example" prefix="e" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%-- 标签内部java类检查来访者是不是盗链,盗链的跳转到网站首页,不是盗链的显示页面--%>
<%-- 把要防盗链的网站和盗链者要重定向到的页面通过标签属性进行配置 --%>
<e:referer site="http://localhost" page="/index.jsp" />
<html>
<head>
<title>开发if标签(是否输出余下JSP案例)</title>
</head>
<body>
凤姐xxxxxxxxxxxx凤姐!!!
</body>
</html>
<%@ 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 }/example/1.jsp">凤姐日记</a>
</body>
</html>
package cn.itcast.web.tag.example;
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 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将JspContext强转成pageContext
PageContext pageContext = (PageContext) this.getJspContext();
// 得到HttpServletReuqest
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
// 1.得到来访者的referer
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);
}
//余下的JSP没有必要执行
throw new SkipPageException();
}
}
}
<?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><!-- 描述文件的uri -->
<tag>
<name>referer</name>
<tag-class>cn.itcast.web.tag.example.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.itcast.web.tag.example.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>