JSP自定义标签

 

一.  标签案例-开发防盗链标签

  盗链是指服务提供商自己不提供服务内容,通过技术手段绕过其他有利益的最终用户界面(如广告),直接在自己的网站上向最终用户提供其他服务提供商的服务内容,片区最终用户的浏览和点击率.受益者不提供资源或是提供很少的资源,而真正的服务提供商却得不到任何的收益.

 

解决途径:

限制引用页

这种防盗链原理是,服务器获取用户提交信息的网站地址,然后和真正的服务端的地址相比较,如果一致则表明是站内提交,或者为自己信任的站点提交,否则视为盗链.

目标:要开发的标签

<s:referrer site="http://localhost:8080" page="/index.jsp"/>

site:受信任站点,只允许次站点的请求.

page:正确的链接页面,发现盗链后将其自动转入此页面.

步骤

1)   标签处理类

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)){

//判断是否盗链

        //根据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();

       }

}

 

2)   描述文件

3)   在内容页面使用标签

二.标签案例-<s:if>标签

标签功能:

<s:if exp="${psw==null }">

    user == null <br>

</s:if>

<%

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

%>   

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

    user != null <br>

</s:if>

 

处理类

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);

        }

    }

}

 

三.标签案例if else 标签

<s:choose >

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

                    aaaaaaaaaa

            </s:when>

            <s:otherwise>

                   bbbbbbbbbb

            </s:otherwise>

</s:choose>

                   

1)编写choose标签,无属性,但有一个成员invoked,要为其编写setter和getter方法,注意doTag中需要输出标签体.

private boolean invoked;

    public boolean isInvoked(){//false:没有子标签被执行 true:某一子标签已被执行

        return invoked;

    }

    public void setInvoked(boolean invoked){

        this.invoked=invoked;

    }

    @Override

    public void doTag() throws JspException, IOException {

        this.getJspBody().invoke(null);

    }

 

2) 编写子标签when标签,有boolean属性exp.

private boolean expr;

    public void stExpr(boolean expr){

        this.expr=expr;

    }

    @Override

    public void doTag() throws JspException, IOException {

        ChooseTag ct=(ChooseTag)this.getParent();

        if(ct.isInvoked()!=true&&expr){

            this.getJspBody().invoke(null);

            ct.setInvoked(true);

        }

    }

 

4)   编写子标签otherwise标签,无属性,无成员变量.

public void doTag() throws JspException, IOException {

       ChooseTag ct=(ChooseTag) this.getParent();

       if(ct.isInvoked()==false){

           this.getJspBody().invoke(null);

           ct.setInvoked(true);

       }

   }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值