『壹』 jsp中,如何判斷驗證碼正確
這個功能通常一共3個頁面:x0dx0a index.jsp是用來登錄用的,在其中顯示驗證碼,即x0dx0a image.jsp是用來生成驗證碼的,有注釋,很詳細,如再不行,你復制到網路就有詳解了。x0dx0a result.jsp 是用來判斷輸入是否正確的。x0dx0a相信你一定能看明白...別忘了採納哦,謝謝。。。x0dx0ax0dx0a【1.index.jsp】x0dx0a<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> x0dx0a
x0dx0a x0dx0ax0dx0a【2.image.jsp】x0dx0a<%@ page contentType="image/JPEG" x0dx0a import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" x0dx0a pageEncoding="GBK"%> x0dx0a<%!Color getRandColor(int fc, int bc) {//給定范圍獲得隨機顏色 x0dx0a Random random = new Random(); x0dx0a if (fc > 255) x0dx0a fc = 255; x0dx0a if (bc > 255) x0dx0a bc = 255; x0dx0a int r = fc + random.nextInt(bc - fc); x0dx0a int g = fc + random.nextInt(bc - fc); x0dx0a int b = fc + random.nextInt(bc - fc); x0dx0a return new Color(r, g, b); x0dx0a }%> x0dx0a<% x0dx0a //設置頁面不緩存 x0dx0a response.setHeader("Pragma", "No-cache"); x0dx0a response.setHeader("Cache-Control", "no-cache"); x0dx0a response.setDateHeader("Expires", 0); x0dx0a x0dx0a // 在內存中創建圖象 x0dx0a int width = 60, height = 20; x0dx0a BufferedImage image = new BufferedImage(width, height, x0dx0a BufferedImage.TYPE_INT_RGB); x0dx0a x0dx0a // 獲取圖形上下文 x0dx0a Graphics g = image.getGraphics(); x0dx0a x0dx0a //生成隨機類 x0dx0a Random random = new Random(); x0dx0a x0dx0a // 設定背景色 x0dx0a g.setColor(getRandColor(200, 250)); x0dx0a g.fillRect(0, 0, width, height); x0dx0a x0dx0a //設定字體 x0dx0a g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); x0dx0a x0dx0a //畫邊框 x0dx0a //g.setColor(new Color()); x0dx0a //g.drawRect(0,0,width-1,height-1); x0dx0a x0dx0a // 隨機產生155條干擾線,使圖象中的認證碼不易被其它程序探測到 x0dx0a g.setColor(getRandColor(160, 200)); x0dx0a for (int i = 0; i < 100; i++) { x0dx0a int x = random.nextInt(width); x0dx0a int y = random.nextInt(height); x0dx0a int xl = random.nextInt(12); x0dx0a int yl = random.nextInt(12); x0dx0a g.drawLine(x, y, x + xl, y + yl); x0dx0a } x0dx0a x0dx0a // 取隨機產生的認證碼(4位數字) x0dx0a String sRand = ""; x0dx0a for (int i = 0; i < 4; i++) { x0dx0a String rand = String.valueOf(random.nextInt(10)); x0dx0a sRand += rand; x0dx0a // 將認證碼顯示到圖象中 x0dx0a g.setColor(new Color(20 + random.nextInt(110), 20 + random x0dx0a .nextInt(110), 20 + random.nextInt(110)));//調用函數出來的顏色相同,可能是因為種子太接近,所以只能直接生成 x0dx0a g.drawString(rand, 13 * i + 6, 16); x0dx0a } x0dx0a x0dx0a // 將認證碼存入SESSION x0dx0a session.setAttribute("code", sRand); x0dx0a x0dx0a // 圖象生效 x0dx0a g.dispose(); x0dx0a x0dx0a // 輸出圖象到頁面 x0dx0a ImageIO.write(image, "JPEG", response.getOutputStream()); x0dx0a%>x0dx0a x0dx0a【3.result.jsp】x0dx0a <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> x0dx0a x0dx0a<% x0dx0a String input=request.getParameter("input"); x0dx0a String code=(String)session.getAttribute("code"); x0dx0a if(input.equals(code)){ x0dx0a out.println("驗證成功!"); x0dx0a }else{ x0dx0a out.println("驗證失敗!"); x0dx0a } x0dx0a%> x0dx0abody>html>『貳』 用一個jsp頁面生成了驗證碼,在登陸界面引入這個界面,怎麼在登陸界面進行驗證碼校驗
生成驗證碼時將實際數據存入session,用戶填寫驗證碼並提交後,將用戶提交的驗證碼與session中的數據做比較就可以了。
『叄』 用jsp實現一個簡單的登錄界面,主要是驗證碼
<html>
<head>
<title>簡單頁面</title>
<script>
function yzm(){
var Num="";
for(var i=0;i<4;i++)
{
Num+=Math.floor(Math.random()*10);
}
document.getElementById("yzphoto").value=Num;
document.getElementById("yzm").value=Num;
}
function userLogin(){
var userName = document.getElementById("userName").value;
var password = document.getElementById("password").value;
var yztext = document.getElementById("yztext").value;
var yzm = document.getElementById("yzm").value;
if(userName != "jq"){
alert("用戶名錯誤");
}else if(password != "123"){
alert("密碼錯誤");
}else if(yztext != yzm){
alert("驗證碼錯誤");
}else{
alert("登陸成功");
}
location.reload();
}
</script>
</head>
<body onLoad="yzm()">
<div style="width:100%;text-align:center">
<h1>用戶登錄</h1>
<table>
<tr>
<td>用戶名:</td>
<td><input id="userName" type="text" value=""/></td>
</tr>
<tr>
<td>密碼:</td>
<td><input id="password" type="password" value=""/></td>
<tr>
<tr><td>
驗證碼:
</td>
<td><input id="yztext" type="text" value=""/><input style="width:50px;background-
color:red;color:blue" type="text" id="yzphoto" value=""/><input type="hidden" id="yzm"
value=""></td></tr>
<tr>
<td colspan="2" align="center"><input onclick="userLogin()" type="button" value="登陸"/></td>
</tr>
</table>
</div>
</body>
</html>
『肆』 jsp中顯示驗證碼的代碼怎麼寫
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Random;
importjavax.imageio.ImageIO;
/*生成驗證碼圖片
*/
publicclassMakeCertPic{
//驗證碼圖片中可以出現的字元集,可以根據需要修改
privatecharmapTable[]={
'a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','q','r',
's','t','u','v','w','x',
'y','z','0','1','2','3',
'4','5','6','7','8','9'
};
/*功能:生成彩色驗證碼圖片
參數wedth為生成圖片的寬度,參數height為生成圖片的高度,參數os為頁面的輸出流
*/
publicStringgetCertPic(intwidth,intheight,OutputStreamos){
if(width<=0)
width=60;
if(height<=0)
height=20;
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文
Graphicsg=image.getGraphics();
//設定背景顏色
g.setColor(newColor(0xDCDCDC));
g.fillRect(0,0,width,height);
//畫邊框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
//隨機產生的驗證碼
StringstrEnsure="";
//4代表4為驗證碼,如果要產生更多位的驗證碼,則加大數值
for(inti=0;i<4;++i){
strEnsure+=mapTable[(int)(mapTable.length*Math.random())];
}
//將認證碼顯示到圖像中,如果要生成更多位的驗證碼,增加drawString語句
g.setColor(Color.black);
g.setFont(newFont("AtlanticInline",Font.PLAIN,18));
Stringstr=strEnsure.substring(0,1);
g.drawString(str,8,17);
str=strEnsure.substring(1,2);
g.drawString(str,20,15);
str=strEnsure.substring(2,3);
g.drawString(str,35,18);
str=strEnsure.substring(3,4);
g.drawString(str,45,15);
//隨機產生15個干擾點
Randomrand=newRandom();
for(inti=0;i<10;i++){
intx=rand.nextInt(width);
inty=rand.nextInt(height);
g.drawOval(x,y,1,1);
}
//釋放圖形上下文
g.dispose();
try{
//輸出圖形到頁面
ImageIO.write(image,"JPEG",os);
}catch(IOExceptione){
return"";
}
returnstrEnsure;
}
}
makeCertPic.jsp頁面用於調用生成驗證碼圖片的JavaBean,並在客戶端顯示,源代碼如下:
<%@pagecontentType="image/jpeg"%><%@pagelanguage="java"pageEncoding="utf-8"%><jsp:useBeanid="image"scope="page"class="securityCode.pic.MakeCertPic"/><%
Stringstr=image.getCertPic(0,0,response.getOutputStream());
//將驗證碼存入session中
session.setAttribute("certCode",str);
%>
下邊是登錄頁面:
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>驗證碼測試登錄頁面</title>
<scripttype="text/javascript">
functionchangeimg()
{
varmyimg=document.getElementById("code");
now=newDate();
myimg.src="makeCertPic.jsp?code="+now.getTime();
}
</script>
</head>
<body>
<center>
<formaction="loginCheck.jsp"method="post"/>
用戶名:<inputtype="text"name="username"/><br>
密 碼:<inputtype="password"name="password"/><br>
驗證碼:<inputtype="text"name="certCode"/>
<imgid="code"src="makeCertPic.jsp"><ahref="javascript:changeimg()">看不清,換一張</a><br>
<inputtype="submit"value="登錄"/>
</form>
</center>
</body>
</html>
『伍』 關於HTML和JSP登陸界面驗證碼的問題
驗證碼原理:驗證碼即為你應用中的一個servlet地址,該地址的響應是一個圖片輸出流,把你生成的驗證碼以圖片的形式響應給頁面即可,下面是一個生成驗證碼的代碼,參考:
packagecom.ec.common.util;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.util.Random;
importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
{
//設置字母的大小,大小
privatestaticFontmFont=newFont("TimesNewRoman",Font.BOLD,20);
staticColorgetRandColor(intfc,intbc){
Randomrandom=newRandom();
if(fc>255)fc=255;
if(bc>255)bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}
/**
*<p>方法名稱:generateImage|描述:生成驗證碼</p>
*@paramrequest
*@paramresponse
*@paramcodeName驗證碼session中的屬性名稱
*@paramwidth驗證碼寬度-不能小於60px
*@paramheight驗證碼高度-不能小於20px
*@throwsIOException
*/
publicstaticvoidgenerateImage(HttpServletRequestrequest,HttpServletResponseresponse,StringcodeName,intwidth,intheight)throwsIOException{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//表明生成的響應是圖片
response.setContentType("image/jpeg");
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphicsg=image.getGraphics();
Randomrandom=newRandom();
g.setColor(getRandColor(200,250));
g.fillRect(1,1,width-1,height-1);
g.setColor(newColor(102,102,102));
g.drawRect(0,0,width-1,height-1);
g.setFont(mFont);
g.setColor(getRandColor(160,200));
//畫隨機線
for(inti=0;i<155;i++){
intx=random.nextInt(width-1);
inty=random.nextInt(height-1);
intxl=random.nextInt(6)+1;
intyl=random.nextInt(12)+1;
g.drawLine(x,y,x+xl,y+yl);
}
//從另一方向畫隨機線
for(inti=0;i<70;i++){
intx=random.nextInt(width-1);
inty=random.nextInt(height-1);
intxl=random.nextInt(12)+1;
intyl=random.nextInt(6)+1;
g.drawLine(x,y,x-xl,y-yl);
}
//生成隨機數,並將隨機數字轉換為字母
StringsRand="";
for(inti=0;i<4;i++){
intitmp=random.nextInt(26)+65;
charctmp=(char)itmp;
sRand+=String.valueOf(ctmp);
g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(String.valueOf(ctmp),15*i+((width-60)/3),random.nextInt(height-20)+20);
}
HttpSessionsession=request.getSession(true);
session.setAttribute(codeName,sRand);
g.dispose();
ImageIO.write(image,"JPEG",response.getOutputStream());
}
}
『陸』 JSP驗證碼問題!
分類: 電腦/網路 >> 程序設計 >> 其他編程語言
問題描述:
寫了一個Servlet用來生成圖片驗證碼,在register.jsp中使用,在regdo.jsp頁面用rand =(String)application.getAttribute("rand");來接生成的驗證碼,用num = request.getParameter("num");來接輸入的驗證碼,進行比較,在本地計算機測試沒問題,可是放上伺服器後偶爾會出現驗證碼輸入錯誤的問題,過段時間就好了,請問這是為什麼?謝謝!
解析:
起servlet服務吧
首頁
<img src=RandomCodeCtrl/>
web.xml
<servlet>
<servlet-name>RandomCodeCtrl</servlet-name>
<servlet-class>.chainway.util.RandomCodeCtrl</servlet-class>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RandomCodeCtrl</servlet-name>
<url-pattern>/RandomCodeCtrl</url-pattern>
</servlet-mapping>
RandomCodeCtrl:
package .chainway.util;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet..HttpServlet;
import javax.servlet..HttpServletRequest;
import javax.servlet..HttpServletResponse;
public class RandomCodeCtrl extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("image/jpeg");
resp.setHeader("Pragma","No-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires", 0);
RandomCode rc = new RandomCode();
try{
rc.getRandcode(req,resp);
}catch(Exception e){
System.err.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
生成類
數字文字圖片驗證碼
package .chainway.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet..HttpServletRequest;
import javax.servlet..HttpServletResponse;
import javax.servlet..HttpSession;
public class RandomCode {
/**
* 隨機取得一個字體
* @param Random random 隨機數
* @return Font 返回一個新字體
*/
private synchronized Font getsFont(Random random){
return new Font("Fixedsys",Font.CENTER_BASELINE,18);
}
/**
* 返回一個隨機顏色
* @param int fc 隨機數
* @param int bc 隨機數
* @param Random random 隨機數
* @return Color 返回一個新顏色
*/
synchronized Color getRandColor(int fc,int bc,Random random){
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc-6);
int g=fc+random.nextInt(bc-fc-4);
int b=fc+random.nextInt(bc-fc-8);
return new Color(r,g,b);
}
/**
* 生成隨機數圖片
*/
public synchronized void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
System.setProperty("java.awt.headless","true");
HttpSession session = request.getSession();
int width=80, height=26;設置圖片大小
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.fillRect(0, 0, width, height);設定邊框
g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
g.setColor(getRandColor(111,133,random));
產生隨機線
for (int i=0;i<11;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
產生隨機點
g.setColor(getRandColor(130,150,random));
產生5個隨機數
String sRand="";
for (int i=0;i<5;i++){
g.setFont(getsFont(random));
g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
String rand=String.valueOf(getRandomString(random.nextInt(36)));
String rand=String.valueOf(getRandomString(random.nextInt(10)));
sRand+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
}
session.removeAttribute("Rand");
session.setAttribute("Rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
}
public synchronized String getRandomString(int num){
String randstring = "***********";
String randstring = "***********abcdefghijklmnopqrstuvwxyz";
return String.valueOf(randstring.charAt(num));
}
}
登陸判斷方法
從session裡面取驗證碼數字進行比較