/*** 1. New class Inherits Simpletagsupport * New 2 properties, add corresponding set method * Overwrite Dotag () method*/Importjava.io.IOException;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.PageContext;Importjavax.servlet.jsp.SkipPageException;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classReferertagextendsSimpletagsupport {PrivateString WebSite; PrivateString Jumppage; Public voidsetwebsite (String webSite) { This. WebSite =WebSite; } Public voidsetjumppage (String jumppage) { This. Jumppage =Jumppage; } @Override Public voidDotag ()throwsjspexception, IOException {pagecontext PageContext= (PageContext) This. Getjspcontext (); HttpServletRequest Request=(HttpServletRequest) pagecontext.getrequest (); HttpServletResponse Response=(HttpServletResponse) pagecontext.getresponse (); String WebRoot=Request.getcontextpath (); //Get RefererString referer = Request.getheader ("Referer"); if(Referer = =NULL|| !referer.startswith (WebSite)) {//It's hotlinking . if(Jumppage.startswith (WebRoot)) {//"/web/index.jsp"Response.sendredirect (jumppage); } Else if(Jumppage.startswith ("/")) {//"/index.jsp"Response.sendredirect (WebRoot +jumppage); } Else{//"index.jsp"Response.sendredirect (WebRoot + "/" +jumppage); } Throw NewSkippageexception ();//if it's hotlinking, throw the exception. } }}
<?XML version= "1.0" encoding= "UTF-8"?><!--file name/web-inf/referer.tld -<taglibxmlns= "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"> <tlib-version>1.0</tlib-version> <Short-name>Anyname</Short-name><!--This value can be set arbitrarily - <URI>AnyUri</URI><!--This URI can be set arbitrarily, but not the same as other. tld Files - <Tag> <name>Referer</name> <Tag-class>De.bvb.web.tag.RefererTag</Tag-class> <body-content>Empty</body-content> <!--do not label body - <attribute> <name>WebSite</name><!--you need to add the previous set of 2 properties - <Required>True</Required><!--required Indicates whether it is necessary - <Rtexprvalue>True</Rtexprvalue><!--Rtexprvalue Indicates whether the value of the property can be used with an El expression - </attribute> <attribute> <name>Jumppage</name> <Required>True</Required> <Rtexprvalue>True</Rtexprvalue> </attribute> </Tag></taglib>
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%@taglib prefix="Referer"URI="/web-inf/referer.tld"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><!--3. Set up the tag library and add the following line tag can be used for anti-theft chain. Website represents a site that does not set up an anti-theft chain, Jumppage represents a page that hotlinking later jumps -<Referer:refererWebSite= "http://localhost"Jumppage= "index.jsp" /><HTML><Head><title>Anti-theft chain via JSP tags</title></Head><Body>An article of certain xxx</Body></HTML>
Javaweb using JSP tags for anti-theft chains