本文目錄一覽:
jsp中,如何判斷驗證碼正確?
這個功能通常一共3個頁面:
index.jsp是用來登錄用的,在其中顯示驗證碼,即img src=”image.jsp”/
image.jsp是用來生成驗證碼的,有注釋,很詳細,如再不行,你複製到百度就有詳解了。
result.jsp 是用來判斷輸入是否正確的。
相信你一定能看明白…別忘了採納哦,謝謝。。。
【1.index.jsp】
%@ page language=”java” import=”java.util.*” pageEncoding=”GBK”%
htmlbody
form method=post action=”result.jsp”
input type=text name=input maxlength=4
img border=0 src=”image.jsp”
input type=”submit” value=”submit”
/form/body/html
【2.image.jsp】
%@ page contentType=”image/JPEG”
import=”java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*”
pageEncoding=”GBK”%
%!Color getRandColor(int fc, int bc) {//給定範圍獲得隨機顏色
Random random = new Random();
if (fc 255)
fc = 255;
if (bc 255)
bc = 255;
int r = fc + random.nextInt(bc – fc);
int g = fc + random.nextInt(bc – fc);
int b = fc + random.nextInt(bc – fc);
return new Color(r, g, b);
}%
%
//設置頁面不緩存
response.setHeader(“Pragma”, “No-cache”);
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, 0);
// 在內存中創建圖象
int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 獲取圖形上下文
Graphics g = image.getGraphics();
//生成隨機類
Random random = new Random();
// 設定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//設定字體
g.setFont(new Font(“Times New Roman”, Font.PLAIN, 18));
//畫邊框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
// 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程序探測到
g.setColor(getRandColor(160, 200));
for (int i = 0; i 100; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取隨機產生的認證碼(4位數字)
String sRand = “”;
for (int i = 0; i 4; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 將認證碼顯示到圖象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 將認證碼存入SESSION
session.setAttribute(“code”, sRand);
// 圖象生效
g.dispose();
// 輸出圖象到頁面
ImageIO.write(image, “JPEG”, response.getOutputStream());
%
【3.result.jsp】
%@ page language=”java” import=”java.util.*” pageEncoding=”GBK”%
htmlbody
%
String input=request.getParameter(“input”);
String code=(String)session.getAttribute(“code”);
if(input.equals(code)){
out.println(“驗證成功!”);
}else{
out.println(“驗證失敗!”);
}
%
bodyhtml
在java頁面上,用jsp,怎樣寫一個驗證碼
//驗證碼生成頁面
%@ page language=”java” import=”java.util.*” pageEncoding=”GBK”%
%@ page import = ” java.awt.*,java.awt.image.*,javax.imageio.* ” %
%@ page import = ” java.io.OutputStream ” %
%!
Color getRandColor( int fc, int bc){
Random random = new Random();
if (fc 255 ) fc = 255 ;
if (bc 255 ) bc = 255 ;
int r = fc + random.nextInt(bc – fc);
int g = fc + random.nextInt(bc – fc);
int b = fc + random.nextInt(bc – fc);
return new Color(r,g,b);
}
%
%
try {
response.setHeader( ” Pragma ” , ” No-cache ” );
response.setHeader( ” Cache-Control ” , ” no-cache ” );
response.setDateHeader( ” Expires ” , 0 );
int width = 60 , height = 20 ;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor( 200 , 250 ));
g.fillRect( 0 , 0 , width, height);
g.setFont( new Font( ” Times New Roman ” ,Font.PLAIN, 18 ));
g.setColor(getRandColor( 160 , 200 ));
for ( int i = 0 ;i 155 ;i ++ )
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt( 12 );
int yl = random.nextInt( 12 );
g.drawLine(x,y,x + xl,y + yl);
}
String sRand = “” ;
for ( int i = 0 ;i 4 ;i ++ ){
String rand = String.valueOf(random.nextInt( 10 ));
sRand += rand;
g.setColor( new Color( 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 ), 20 + random.nextInt( 110 )));
g.drawString(rand, 13 * i + 6 , 16 );
}
session.setAttribute(“vcode” ,sRand);
g.dispose();
ImageIO.write(image, “jpg” ,os);
os.flush();
os.close();
os = null ;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
catch (IllegalStateException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
} %
//驗證碼使用頁面
%@ 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%”
titleMy 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”
!–
link rel=”stylesheet” type=”text/css” href=”styles.css”
—
script type=”text/javascript”
function refresh(){
var co=document.getElementById(“code”);
co.src=”vcode.jsp?vcode=”+Math.random();
}
/script
/head
body
用戶名:input type=”text” name=”name”br/
img id=”code” title=”驗證碼” src=”vcode.jsp” a href=”javascript:refresh();”換一張/a
/body
/html
java中如何在同一個jsp頁面中判斷圖片驗證碼與輸入的是否一致
這個問題很簡單的。我想你動態產生的驗證碼一定是一個單獨的頁面然後再把這個頁面導入到你的輸入信息的頁面的。你打開你的動態生成驗證碼的頁面。在裏面你一定可以找到一個session.setAttribute(“”); 這個方法是把你驗證碼的值保存在session裏面。根據你的意思在用js彈出來驗證. 首先你在頁面中加一小腳本% Stirng vcode= (String)session.getAttribute(“驗證碼Key”) %然後在你的提交按鈕中加一個onClick事件… 請輸入驗證碼:input type=”text” id=”txtvcode” name=”txtvcode” / input type=”button” name=”text” id=”text” value=”提交” onClick=”checkVcode(‘%= vcode%’)” /這樣就可以把你動態生成的驗證碼值傳入到js的函數中去了。 script function checkVcode(vcode){ var txtvcode= document.getElementById(“txtvcode”).value; if(vcode==txtvcode){ alert(“輸入驗證碼正確”); }else{ alert(“輸入驗證碼錯誤”); } } /script人在網吧。沒工具調試。我想這些代碼不會出問題的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/244913.html