tagServlet代碼如下:
@requires_authorizationpackage 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 tagFangDaoLian 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 pc = (PageContext) this.getJspContext(); HttpServletRequest request = (HttpServletRequest) pc.getRequest(); String referer = request.getHeader("referer"); HttpServletResponse response = (HttpServletResponse) pc.getResponse(); System.out.println(request.getContextPath()); 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); } throw new SkipPageException(); } }}tld配置文件代碼如下:
@requires_authorization<?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"> <tlib-version>1.0</tlib-version> <short-name>showlist</short-name> <uri>/tag</uri><tag> <name>untheft</name> <tag-class>example.tagFangDaoLian</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></taglib>jsp頁面調用如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="m" uri="/tag" %>** <m:untheft site="http://localhost" page="untheft.jsp"/>** <%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'main.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> </head> <body> <h3>選項</h3> <a href="jsp/save.jsp">添加新員工</a> <a href="${pageContext.request.contextPath}/servlet/selectPageServlet?pageNum=1">查看所有員工</a> </body></html>當用戶以http:localhost(即site,可以自己更改屬性)為請求頭時,標簽將執行,自動轉入page界面[^footnote].
新聞熱點
疑難解答