『壹』 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里面取验证码数字进行比较