JAVA做驗證碼圖片自CSDN)

來源:互聯網
上載者:User

網上搜尋了一下;風格都太簡單;

逛了下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="http://images.cnblogs.com/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;

驗證碼圖片終於可以定製了;想什麼風格就什麼風格;自己說了算了;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.