posts - 0,  comments - 19,  trackbacks - 0
生成验证码的jsp页面createCode.jsp:


<%@ page language="java" contentType="image/jpegRoomAction.javaRoomAction.javaRoomAction.java; charset=utf-8" pageEncoding="utf-8"%>
<%@page import="java.io.*,java.util.*,com.sun.image.codec.jpeg.*,java.awt.*,java.awt.image.*"%>
<%
    String s 
= "";
    
int intCount = 0;
    
/**
     * 验证码宽度
     
*/
    
int width = 100;
    
/**
     * 验证码高度
     
*/
    
int height = 25;
    
/**
     * 验证码字符集
     
*/
    
char[] code = new char[] { 'A''B''C''D''E''F''G''H',
            
'I''J''K''L''M''N''P''Q''R''S''T''U',
            
'V''W''X''Y''Z''a''b''c''d''e''f''g',
            
'h''i''j''k''l''m''n''p''q''r''s''t',
            
'u''v''w''x''y''z''2''3''4''5''6''7',
            
'8''9' };

    
/**
     *  创建一个随机数生成器类
     
*/
    Random random 
= new Random();

    
// 随机产生codeCount数字的验证码。   
    for (int i = 0; i < 4; i++) {
        
// 得到随机产生的验证码数字。   
        String strRand = String.valueOf(code[random
                .nextInt(code.length)]);

        
// 将产生的四个随机数组合在一起。   
        s = s + strRand;
    }

    
// 保存入session,用于与用户的输入进行比较.
    
// 注意比较完之后清除session.

    session.setAttribute(
"rand", s);

    response.setContentType(
"image/gif");

    
// 定义图像buffer   
    BufferedImage image = new BufferedImage(width, height,
            BufferedImage.TYPE_INT_RGB);
    Graphics2D gra 
= image.createGraphics();
    
// 将图像填充为白色   
    gra.setColor(Color.WHITE);
    gra.fillRect(
00, width, height);

    
// 创建字体,字体的大小应该根据图片的高度来定。  

    
//字体对象构造方法public Font(String familyName,int style,int size)
    
// familyName字体名;字体名可以分成两大类:中文字体:宋体、楷体、黑体等;英文字体:Arial、Times New Roman等等;
    
// style风格。PLAIN普通字体,BOLD(加粗),ITALIC(斜体),Font.BOLD+ Font.ITALIC(粗斜体)
    
//size 大小

    Font font 
= new Font("宋体", Font.BOLD + Font.ITALIC, height - 1);//

    
// 设置字体。   
    gra.setFont(font);

    
// 画边框。   
    gra.setColor(getColor());
    gra.drawRect(
00, width - 1, height - 1);

    
// 随机产生干扰线,使图象中的认证码不易被其它程序探测到。   
    gra.setColor(Color.BLACK);
    
for (int i = 0; i < 50; i++) {
        
int x = random.nextInt(width);
        
int y = random.nextInt(height);
        
int xl = random.nextInt(5);
        
int yl = random.nextInt(5);
        gra.setColor(getColor());
        gra.drawLine(x, y, x 
+ xl, y + yl);
    }

    
// 输出数字
    char c;

    
for (int i = 0; i < 4; i++) {

        c 
= s.charAt(i);

        gra.drawString(c 
+ "", i * 25 + 420); // 25为宽度,11为上下高度位置

    }

    OutputStream toClient 
= response.getOutputStream();
    JPEGImageEncoder encoder 
= JPEGCodec.createJPEGEncoder(toClient);
    encoder.encode(image);

    toClient.close();

    out.clear();
    out 
= pageContext.pushBody();
%>
<%!private Color getColor() {
        Random random 
= new Random();
        int red = 0, green = 0, blue = 0;
        
// 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。   
        red = random.nextInt(255);
        green 
= random.nextInt(255);
        blue 
= random.nextInt(255);
        
return new Color(red, green, blue);
    }
%>



引用验证码的的jsp页面regist.jsp:

<script type="text/javascript" >
//<![CDATA[
function idenfunc()
{
    $(
"#input06").val("");
    $(
"#idenimage").attr("src","/html/member/createMa.jsp?temp="+new Date());
    
}

function toRegist(){
    $.get('/getyzqqLoginAction.do?id='+$('#input06').val(),function(data){
        if(data=='1'){
            alert('效验码不正确,请重新输入.');
            $('#errorM').html('效验码不正确,请重新输入.');
            return;
        }
    })
}
//]]>
</script>

<html>
<head><title></title></head>
<body>
<div>
<s:form>
<table>
<tr>
   
<th><label for="input06">校验码:</label></th>
    
<td><input type="text" class="input01" id="input06"  /></td>
    
<td><img id="idenimage" src="/html/member/createMa.jsp" alt="" /> 
    
<a href="#" onclick="idenfunc();" class="blue">看不清</a>
    
</td>
</tr>
</table>
</s:form>
<div class="infobox_register">
                <p>
                    <img onclick="toRegist();return false;" style="cursor:pointer" src="/images/button/btn01_regist_info.gif" alt="" />
                </p>
</div>
</body>
</html>

java验证代码:

public void getyz()throws Exception{
        HttpServletRequest request 
= ServletActionContext.getRequest();
        String identity 
= request.getParameter("id"!= null ? request.getParameter("id") : "" ;
        HttpSession session 
= request.getSession();
        String realidentity
=(String)session.getAttribute("rand");
        HttpServletResponse response 
= ServletActionContext.getResponse();
        response.setCharacterEncoding(
"utf-8");
        response.setContentType(
"text/html");
        
if(!identity.trim().equalsIgnoreCase(realidentity.trim())){
            response.getWriter().write(
"1");
            response.getWriter().close();
        }
else{
            session.removeAttribute(
"rand");
            response.getWriter().write(
"0");
            response.getWriter().close();
        }
    }
posted on 2012-04-13 15:19 canry Tong 阅读(722) 评论(3)  编辑  收藏 所属分类: 验证码生成方式

FeedBack:
# re: 在jsp页面中生成验证码
2012-07-18 15:30 | rr
rrrrr  回复  更多评论
  
# re: 在jsp页面中生成验证码
2012-07-18 15:48 | rr
你提交的时候怎么验证验证码的?  回复  更多评论
  
# re: 在jsp页面中生成验证码
2012-07-18 16:07 | canry Tong
@rr
我完善了一下  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿

文章分类

文章档案

搜索

  •  

最新评论