第一步:编写一个防盗标签处理器类RefererTag.java
public class RefererTag extends SimpleTagSupport {
private String page;
private String site;
public void setPage(String page) {
this.page = page;
}
public void setSite(String site) {
this.site = site;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext=(PageContext) this.getJspContext();
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
HttpServletResponse response= (HttpServletResponse) pageContext.getResponse();
String referer=request.getHeader("referer");
if(referer==null || !referer.startsWith(site)){
String cp=request.getContextPath();
if(page.startsWith(cp))
{ response.sendRedirect(page);
}else if (page.startsWith("/")) {
response.sendRedirect(cp+page);
}else
{
response.sendRedirect(cp+"/"+page);
}
throw new SkipPageException();
}
else
super.doTag();
}
}
第二步:在WEB-INF编写一个*.tld标签描述文件 view.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>SimpleTagLibrary</short-name>
<uri>/jsp2-tagexample</uri>
<tag>
<name>referer</name>
<tag-class>cn.itcast.web.tag.eaxmple.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>yes</required>
</attribute>
<attribute>
<name>page</name>
<required>yes</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
第三步:编写一个jsp并且导入自定义标签
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri ="/jsp2-tagexample" prefix="fix"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<fix:referer site="http://localhost" page="/index.jsp"></fix:referer>
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
第四步:测试...
public class RefererTag extends SimpleTagSupport {
private String page;
private String site;
public void setPage(String page) {
this.page = page;
}
public void setSite(String site) {
this.site = site;
}
@Override
public void doTag() throws JspException, IOException {
PageContext pageContext=(PageContext) this.getJspContext();
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
HttpServletResponse response= (HttpServletResponse) pageContext.getResponse();
String referer=request.getHeader("referer");
if(referer==null || !referer.startsWith(site)){
String cp=request.getContextPath();
if(page.startsWith(cp))
{ response.sendRedirect(page);
}else if (page.startsWith("/")) {
response.sendRedirect(cp+page);
}else
{
response.sendRedirect(cp+"/"+page);
}
throw new SkipPageException();
}
else
super.doTag();
}
}
第二步:在WEB-INF编写一个*.tld标签描述文件 view.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>SimpleTagLibrary</short-name>
<uri>/jsp2-tagexample</uri>
<tag>
<name>referer</name>
<tag-class>cn.itcast.web.tag.eaxmple.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>yes</required>
</attribute>
<attribute>
<name>page</name>
<required>yes</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
第三步:编写一个jsp并且导入自定义标签
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri ="/jsp2-tagexample" prefix="fix"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<fix:referer site="http://localhost" page="/index.jsp"></fix:referer>
<html>
<head>
<title>My JSP '1.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
第四步:测试...