自定义标签实例

JSP自定义标签实例

1、 防盗链

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

(1)解决途径之一 ——限制引用页

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

目标:要开发的标签

<class3g:referrer site=http://drinkeye:8080 page=”/index.jsp”>

A、Site:受信任点,只允许次站点的请求

B、Page:正确的链接页面,发现盗链后将其自动转入此页面的步骤

C、源码为:

public class RefererTag extends SimpleTagSupport{

private String site;

private String page;

public void setSite(String site) {

this.site = site;

}

public void setPage(String page) {

this.page = page;

}

public void doTag() throws JspException, IOException {

//还原处理

PageContext pc = (PageContext)this.getJspContext();

HttpServletRequest request = (HttpServletRequest)pc.getRequest();

String referer = request.getHeader("referer");

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

HttpServletResponse response = (HttpServletResponse)pc.getResponse();

String contextPath = request.getContextPath();

System.out.println(contextPath);

if(page.startsWith(contextPath)){

response.sendRedirect(page);

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

response.sendRedirect(contextPath+page);

}else{

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

}

throw new SkipPageException();

}

}

}

2、标签案例-<c:if>标签

标签功能:

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

user == null <br>

</class3g:if>

<%

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

%>

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

user != null <br>

</class3g: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);

}

}

}

3、标签案例if else 标签

<class3g:choose >

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

aaaaaaaaaa

</class3g:when>

<class3g:otherwise>

bbbbbbbbbbbbbbb

</class3g:otherwise>

</class3g:choose>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值