1. Write this class first.
package itcast.lxd.web.simpleTagAttrib;import java.io.IOException;import java.util.Date;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.JspFragment;import javax.servlet.jsp.tagext.SimpleTagSupport;public class referDemo extends SimpleTagSupport{String webSiteReferer ;String webIndex;public void setWebSiteReferer(String webSiteReferer){this.webSiteReferer = webSiteReferer;}public void setWebIndex(String webIndex){this.webIndex = webIndex;}public void doTag() throws JspException, IOException{PageContext pageContext =(PageContext)this.getJspContext();HttpServletRequest request =(HttpServletRequest)pageContext.getRequest();HttpServletResponse response =(HttpServletResponse)pageContext.getResponse();if(request.getHeader("referer")==null||!request.getHeader("referer").startsWith(webSiteReferer)){response.sendRedirect(webIndex);throw new SkipPageException();}else{}}}
This tag has two attributes. One is the site, which means that the anti-leech feature can be accessed only from a connection. The second page indicates where to jump to access the page from other locations.
Jspcontext must be transformed to pagecontext to obtain the required objects. Then, you can know the access Location Based on the referer header in the browser. Then, you can set the input format for the label-friendly, next we need to describe this tag in the TLD file.
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"><taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>refererTest</short-name> <uri>http://www.itcast.lxd.com.referer</uri> <display-name>JSTL core</display-name> <description>JSTL 1.0 core library</description> <tag> <name>referExample</name> <tag-class>itcast.lxd.web.simpleTagAttrib.referDemo</tag-class> <body-content>empty</body-content> <attribute> <name>webSiteReferer</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>webIndex</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>
3.0 use this label
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <% @ taglib uri =" http://www.itcast.lxd.com.referer "prefix =" refertest "%> <% string Path = request. getcontextpath (); string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; %> <refertest: referexample websitereferer = "http: // localhost" webindex = "http://www.baidu.com"/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>