生成登陆验证码

来源:互联网 发布:批处理静默安装软件 编辑:程序博客网 时间:2024/06/07 10:04

要用到的包:SWUtil.jar

要用到的tld文件:taglibs-image.tld

 

 

创建一个jsp文件.

首先写入如下代码

  1. <%
  2. response.setHeader("Pragma", "No-cache");
  3. response.setHeader("Cache-Control", "no-cache");
  4. response.setDateHeader("Expires", 0);
  5. %>

这样可以禁止浏览器在本地计算机上缓存该页面

 

 

设置编码格式:

  1. <%@ page contentType="text/html; charset=GBK" language="java"%>

引用tld文件:

  1. <%@ taglib uri="http://com.sourceware.com/taglibs/image-1.0" prefix="img" %>

生成一个4位的随即数,并添加进session属性中:

  1. <%
  2. int num = (int) java.lang.Math.round(java.lang.Math.random() * 8999);
  3.  String sRand = "" + (1000 +num);
  4.     session.setAttribute("userInfo.authcode",sRand);
  5. %>

生成图片:

  1. <img:image src="/images/auth.jpg" refresh="true">
  2. <img:text text="<%=sRand.substring(0,1)%>" x="5" y="-2" font="Times New Roman" bold="true" size="18" color="0x993399" italic="true" />
  3. <img:text text="<%=sRand.substring(1,2)%>" x="17" y="-2" font="Times New Roman" bold="false" size="18" color="0x660033" italic="false" />
  4. <img:text text="<%=sRand.substring(2,3)%>" x="27" y="-2" font="Times New Roman" bold="true" size="18" color="0xcc3366" italic="true" />
  5. <img:text text="<%=sRand.substring(3,4)%>" x="40" y="-2" font="Times New Roman" bold="false" size="18" color="0x990099" italic="false" />
  6. <%if(num<2000){%>
  7.     <img:grayscale brightness="90" />
  8. <%}%>
  9. </img:image>
原创粉丝点击