JavaWeb -- 自定义标签实例, 防盗链, JSTL 核心标签
1. 自定义标签应用实例1: 防盗链
标签处理类
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; } @Override public void doTag() throws JspException, IOException { PageContext context = (PageContext) this.getJspContext(); HttpServletRequest request = (HttpServletRequest) context.getRequest(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); String referer = request.getHeader("referer"); //获得来访者链接 //System.out.println("referer: " + referer); 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文件
<tag> <name>RefererTag</name> <tag-class>com.kevin.web.tag.RefererTag</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>
应用:该网页不能直接访问, 必须从首页超链接跳转进来
<kevin:RefererTag site="http://192.168.1.113" page="index.jsp" />
2. JSTL核心标签使用 示例
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JSTL测试</title> </head> <body> <br /> ----------------- c:out 标签 ------------------------------------<br /> <c:out value="<a href=''> </a>" default="默认值" escapeXml="true"></c:out> <br /> ----------c:set 标签 可操作各个域 javabean map集合-------------<br /> <c:set var="data1" value="data1xxx" scope="page" /> ${data1} <br /> <% Map map = new HashMap(); request.setAttribute("map", map); %> <c:set target="${map}" property="dd" value="xxxxx"></c:set> ${map.dd} <br /> <br /> -------- c:remove 标签 从域中删除域中属性------------------<br /> ${data1} <br /> <c:remove var="data1" /> ${data1} <br /> <br /> -------- c:catch 标签 抓取异常-----------------<br /> <c:catch var="exception"> <% int i = 5/0; %> </c:catch> ${exception.message} <br /> <br /> -------- c:if 标签 条件判断-----------------<br /> <c:if var="result" test="${user==null}"> c:if test <br/> </c:if> ${result} <br /> <br /> -------- c:choose 标签 条件判断-----------------<br /> <c:choose> <c:when test="${user!=null}">用户登录<br/> </c:when> <c:otherwise>无用户登录<br /> </c:otherwise> </c:choose> <br /> -------- c:foreach 标签 -----------------<br /> <% Map map2 = new HashMap(); map2.put("aaaa", "aaaaa"); map2.put("1111", "11111"); request.setAttribute("map2", map2); %> <!-- var对应的是 每次获得的对象 存到域里, 对应的关键字 是var --> <c:forEach var="entry" items="${map2}"> ${entry.key} : ${entry.value} <br /> </c:forEach> <c:forEach var="num" begin="1" end="9" step="1"> ${num} <br /> </c:forEach> <br /> -------- c:foreach varStatus实现表格间色---------------<br /> <% List list = new ArrayList(); list.add("aaaaaaaaaa"); list.add("bbbbbbbbbb"); list.add("cccccccccc"); list.add("dddddddddd"); list.add("eeeeeeeeee"); request.setAttribute("list", list); %> <style> .even{background-color: red;} .odd{background-color: green;} tr:hover{background-color: blue;} </style> <%-- varStatus获取到一个对象,该对象记录有是第几次迭代--%> <table border="1px" width="30%"> <c:forEach var="row" items="${list}" varStatus="status"> <tr class="${status.count%2==0?'even':'odd'}"> <td>${row}</td> </tr> </c:forEach> </table> <br /> --- c:url 构造一个URL地址,并且会带上sessionID,参数自动编码 -----<br /> <c:url var="url" value="/index.jsp"> <c:param name="name">中国</c:param> </c:url> <a href="${url}" >点点</a> <br /> <br /> -------- c:forTokens 字符串分割---------------<br /> <% request.setAttribute("data2", "a,b,c,d,e,f"); %> <c:forTokens var="str" items="${data2}" delims=","> ${str} <br /> </c:forTokens> <br /> -------- c:redirect 重定向---------------<br /> <%--<c:redirect url="/index.jsp"></c:redirect> --%> </body> </html>