Use custom tags in JSP to prevent leeching (Chuanzhi podcast study notes)

Source: Internet
Author: User

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> 









Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.