首先給你個驗證碼模闆,檔案名為Num.jsp
<% @ page pageEncoding = " gbk " contentType = " image/jpeg " import = " javax.imageio.*,java.util.*,java.awt.image.*,java.awt.* " %>
window.self.location.reload( true );
<%!
// 在此處 擷取并生成随機顔色
Color getRandColor(Random random, int ff, int cc) {
if (ff > 255 )
ff = 255 ;
if (cc > 255 )
cc = 255 ;
int r = ff + random.nextInt(cc - ff);
int g = ff + random.nextInt(cc - ff);
int b = ff + random.nextInt(cc - ff);
return new Color(r, g, b);
} %>
<%
response.setHeader( " Pragma " , " No-cache " );
response.setHeader( " Cache-Control " , " no-cache " );
response.setDateHeader( " Expires " , 0 );
int width = 60 ; // 定義驗證碼圖檔的長度
int height = 20 ; // 定義驗證碼圖檔的寬度
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(random, 200 , 250 ));
g.fillRect( 0 , 0 , width, height);
g.setFont( new Font( " Times New Roman " ,Font.PLAIN, 18 ));
// 定義字型形式
g.setColor(getRandColor(random, 160 , 200 ));
for ( int i = 0 ;i < 155 ;i ++ )
{
int i_x = random.nextInt(width);
int i_y = random.nextInt(height);
int i_xl = random.nextInt( 12 );
int i_yl = random.nextInt( 12 );
g.drawLine(i_x,i_y,i_x + i_xl,i_y + i_yl);
}
// 用線條畫背景
String s_Rand = "" ;
for ( int i = 0 ;i < 4 ;i ++ )
{
String rand = String.valueOf(random.nextInt( 10 ));
s_Rand += rand;
g.setColor( new Color( 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 )));
g.drawString(rand, 13 * i + 6 , 16 );
}
// 産生4位随機碼
session.setAttribute( " rand " ,s_Rand);
// 将驗證碼存入Session中
g.dispose();
ImageIO.write(image, " JPEG " , response.getOutputStream());
// 輸出驗證圖檔
out .clear();
out = pageContext.pushBody();
%>
重要的是如何調用這個驗證碼,而且單擊驗證碼讓它自動換一張,看下面代碼,檔案名為test.jsp
<% @ page language = " java " import = " java.util.* " pageEncoding = " gbk " %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
%>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html >
< head >
< base href = " <%=basePath%> " >
< title > My JSP ' index.jsp ' starting page </ title >
< meta http - equiv = " pragma " content = " no-cache " >
< meta http - equiv = " cache-control " content = " no-cache " >
< meta http - equiv = " expires " content = " 0 " >
< meta http - equiv = " keywords " content = " keyword1,keyword2,keyword3 " >
< meta http - equiv = " description " content = " This is my page " >
< script type = " text/javascript " >
function change()
{
var img = document.getElementById( " code " );
img.src = " Num.jsp?date= " + new Date(); // 此處很重要,不加後面的就不會變
}
</ script >
</ head >
< body >
< center >
< form id = " dForm " method = post action = "" >
使用者名: < input name = " username " >< br >
密 & nbsp;碼: < input type = " password " name = " password " >< br >
校驗碼: < input id = " check " size = " 10 " >
< img id = " code " border = 0 src = " Num.jsp " onclick = " change() " />< br >
< input type = " reset " value = " 重置 " >
< input type = " submit " value = " 送出 " >
</ form >
</ center >
</ body >
</ html >
轉載于:https://www.cnblogs.com/lhxfzu/archive/2011/04/28/2032254.html