程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp自定義標簽-3

jsp自定義標簽-3

編輯:關於JSP

 

1、標簽案例-開發防盜鏈標簽

盜鏈是指服務提供商自己不提供服務的內容,通過技術手段繞過其它有利益的最終用戶界面(如廣告),直接在自己的網站上向最終用戶提供其它服務提供商的服務內容,騙取最終用戶的浏覽和點擊率。受益者不提供資源或提供很少的資源,而真正的服務提供商卻得不到任何的收益。

解決途徑之一——限制引用頁

這種防盜鏈原理是,服務器獲取用戶提交信息的網站地址,然後和真正的服務端的地址相比較,如果一致則表明是站內提交,或者為自己信任的站點提交,否則視為盜鏈。

目標:要開發的標簽

<class3g:referer site="http://drinkeye:8080" page="/index.jsp"/>

site:受信任站點,只允許次站點的請求

page:正確的鏈接頁面,發現盜鏈後將其自動轉入此頁面

步驟

1)        標簽處理類

 

<span style="font-size:16px;">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"); 

 

   System.out.println(request.getContextPath()); 

 

    

 

   if(referer==null | !referer.startsWith(site)){ 

 

//判斷是否盜鏈 

 

</span> 

 

    

 

<span style="font-size:16px;"> //根據page屬性值,講鏈接重定向指訪問被盜鏈內容的正確頁面 

 

      String contextPath = request.getContextPath(); 

 

      if(page.startsWith(contextPath)){ 

 

        response.sendRedirect(page); 

 

      }else if(page.startsWith("/")){ 

 

        response.sendRedirect(contextPath + page);  

 

      }else{ 

 

        response.sendRedirect(contextPath + "/" + page); 

 

      } 

 

      throw new SkipPageException(); 

 

   } 

 

</span> 

 

 

 

2)        描述文件

3)        在內容頁面使用標簽

2、標簽案例-<c:if>標簽

標簽功能:

 

<span style="font-size:16px;"><class3g:if exp="${psw==null }"> 

 

    user == null <br> 

 

</class3g:if> 

 

     

 

<% 

 

    session.setAttribute("user","Tom"); 

 

%>     

 

<class3g:if exp="${user!=null }"> 

 

    user != null <br> 

 

</class3g:if> 

</span> 

處理類

 

<span style="font-size:16px;">public class MyIfTag extends SimpleTagSupport { 

 

  

 

    private boolean exp; 

 

     

 

    public void setExp(boolean exp) { 

 

       this.exp = exp; 

 

    } 

 

  

 

    public void doTag() throws JspException, IOException { 

 

       if(exp){ 

 

           JspFragment jf = this.getJspBody(); 

 

           jf.invoke(null); 

 

       } 

 

    } 

 

  

 

</span> 

3、標簽案例if else 標簽

 

<span style="font-size:16px;"><class3g:choose > 

 

      <class3g:when exp="${user!=null }"> 

 

         aaaaaaaaaa 

 

      </class3g:when> 

 

      <class3g:otherwise> 

 

         bbbbbbbbbbbbbbb 

 

      </class3g:otherwise> 

 

</class3g:choose> 

 

</span> 

 

 

步驟:

l  編寫choose標簽,無屬性,但有一個成員invoked,要為其編寫setter和getter方法,注意doTag中需要輸出標簽體

l  編寫子標簽when標簽,有boolean屬性exp,

l  編寫子標簽otherwise標簽,無屬性,無成員變量

 

摘自 耗子的程序員之路

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved