要用到的包:SWUtil.jar
要用到的tld文件:taglibs-image.tld
创建一个jsp文件.
首先写入如下代码
- <%
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- %>
这样可以禁止浏览器在本地计算机上缓存该页面
设置编码格式:
- <%@ page contentType="text/html; charset=GBK" language="java"%>
引用tld文件:
- <%@ taglib uri="http://com.sourceware.com/taglibs/image-1.0" prefix="img" %>
生成一个4位的随即数,并添加进session属性中:
- <%
- int num = (int) java.lang.Math.round(java.lang.Math.random() * 8999);
- String sRand = "" + (1000 +num);
- session.setAttribute("userInfo.authcode",sRand);
- %>
生成图片:
- <img:image src="/images/auth.jpg" refresh="true">
- <img:text text="<%=sRand.substring(0,1)%>" x="5" y="-2" font="Times New Roman" bold="true" size="18" color="0x993399" italic="true" />
- <img:text text="<%=sRand.substring(1,2)%>" x="17" y="-2" font="Times New Roman" bold="false" size="18" color="0x660033" italic="false" />
- <img:text text="<%=sRand.substring(2,3)%>" x="27" y="-2" font="Times New Roman" bold="true" size="18" color="0xcc3366" italic="true" />
- <img:text text="<%=sRand.substring(3,4)%>" x="40" y="-2" font="Times New Roman" bold="false" size="18" color="0x990099" italic="false" />
- <%if(num<2000){%>
- <img:grayscale brightness="90" />
- <%}%>
- </img:image>