JAVA做验证码图片(转自CSDN)

网上搜索了一下;风格都太简单;

逛了下apache;发现有个开原的项目:

Jakarta Project: Image Tag library

 

非常好,下载下来一是;不错;

JSP代码如下:
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@ taglib uri="http://com.sourceware.com/taglibs/image-1.0" prefix="img" %>
<%
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="7" y="-1" font="Times New Roman" bold="false" size="22" color="0x993399" italic="true" />
<img:text text="<%=sRand.substring(1,2)%>" x="17" y="-1" font="Times New Roman" bold="false" size="22" color="0x660033" italic="false" />
<img:text text="<%=sRand.substring(2,3)%>" x="27" y="-1" font="Times New Roman" bold="true" size="18" color="0xcc3366" italic="true" />
<img:text text="<%=sRand.substring(3,4)%>" x="37" y="-1" font="Times New Roman" bold="false" size="22" color="0x990099" italic="false" />


<%if(num<4500){%>
 <img:grayscale brightness="90" />
<%}%>

</img:image>

<!-- Times New Roman -->

 

不过,这个标签用来做验证码图片有个问题;如果返回登陆页面;验证码图片不会更新;

检查原因发现org.apache.taglibs.image.ImageSevlet 类没有设置:

response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

 

修改他的代码;重新部署;一切ok;

验证码图片终于可以定制了;想什么风格就什么风格;自己说了算了;

posted @ 2007-04-11 15:20  Max Woods  阅读(633)  评论(0编辑  收藏  举报