日韩中文在线中文网三级,在线观看免费国产小视频,粉嫩一区二区三区http://www.aygfsteel.com/tyjava/category/51393.html在學習中進步,在進步中學習!zh-cnWed, 18 Jul 2012 22:03:46 GMTWed, 18 Jul 2012 22:03:46 GMT60在jsp頁面中生成驗證碼http://www.aygfsteel.com/tyjava/articles/374078.htmlcanry Tongcanry TongFri, 13 Apr 2012 07:19:00 GMThttp://www.aygfsteel.com/tyjava/articles/374078.htmlhttp://www.aygfsteel.com/tyjava/comments/374078.htmlhttp://www.aygfsteel.com/tyjava/articles/374078.html#Feedback3http://www.aygfsteel.com/tyjava/comments/commentRss/374078.htmlhttp://www.aygfsteel.com/tyjava/services/trackbacks/374078.html生成驗證碼的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' };

    
/**
     *  創(chuàng)建一個隨機數(shù)生成器類
     
*/
    Random random 
= new Random();

    
// 隨機產(chǎn)生codeCount數(shù)字的驗證碼。   
    for (int i = 0; i < 4; i++) {
        
// 得到隨機產(chǎn)生的驗證碼數(shù)字。   
        String strRand = String.valueOf(code[random
                .nextInt(code.length)]);

        
// 將產(chǎn)生的四個隨機數(shù)組合在一起。   
        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);

    
// 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來定。  

    
//字體對象構(gòu)造方法public Font(String familyName,int style,int size)
    
// familyName字體名;字體名可以分成兩大類:中文字體:宋體、楷體、黑體等;英文字體:Arial、Times New Roman等等;
    
// style風格。PLAIN普通字體,BOLD(加粗),ITALIC(斜體),F(xiàn)ont.BOLD+ Font.ITALIC(粗斜體)
    
//size 大小

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

    
// 設(shè)置字體。   
    gra.setFont(font);

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

    
// 隨機產(chǎn)生干擾線,使圖象中的認證碼不易被其它程序探測到。   
    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);
    }

    
// 輸出數(shù)字
    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;
        
// 產(chǎn)生隨機的顏色分量來構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。   
        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="http://www.aygfsteel.com/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();
        }
    }


canry Tong 2012-04-13 15:19 發(fā)表評論
]]>
在java代碼中生成驗證碼http://www.aygfsteel.com/tyjava/articles/371993.htmlcanry Tongcanry TongFri, 16 Mar 2012 00:58:00 GMThttp://www.aygfsteel.com/tyjava/articles/371993.htmlhttp://www.aygfsteel.com/tyjava/comments/371993.htmlhttp://www.aygfsteel.com/tyjava/articles/371993.html#Feedback0http://www.aygfsteel.com/tyjava/comments/commentRss/371993.htmlhttp://www.aygfsteel.com/tyjava/services/trackbacks/371993.html<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>無標題頁</title>
    
<style type="text/css">
        .code
        {
            background
-image:url(code.jpg);
            font
-family:Arial;
            font
-style:italic;
            color:Red;
            border:
0;
            padding:2px 3px;
            letter
-spacing:3px;
            font
-weight:bolder;
        }
        .unchanged
        {
            border:
0;
        }
    
</style>
    
<script language="javascript" type="text/javascript">
   
    var code ; 
//在全局 定義驗證碼
    function createCode()
      {
        code 
= "";
      var codeLength 
= 6;//驗證碼的長度
      var checkCode = document.getElementById("checkCode");
      var selectChar 
= new Array(0,1,2,3,4,5,6,7,8,9,'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');//所有候選組成驗證碼的字符,當然也可以用中文的
       
      
for(var i=0;i<codeLength;i++)
        {
     
       
      var charIndex 
= Math.floor(Math.random()*36);
        code 
+=selectChar[charIndex];
     
     
        }
      
if(checkCode)
        {
          checkCode.className
="code";
          checkCode.value 
= code;
        }
     
      }
   
      function validate ()
      {
      var inputCode 
= document.getElementById("input1").value;
      
if(inputCode.length <=0)
        {
            alert(
"請輸入驗證碼!");
        }
      
else if(inputCode != code )
        {
          alert(
"驗證碼輸入錯誤!");
          createCode();
//刷新驗證碼
        }
      
else
        {
          alert(
"^-^ OK");
        }
     
        }
      function a(){
        createCode();
       }
    
</script>
</head>
<body onload="a()">
<form  action="#">
    
<input  type="text" id="input1" />
    
<input type="text"  id="checkCode" class="unchanged" style="width: 80px"  /><br />
    
<input id="Button1"  onclick="a();" type="button" value="刷新" /> 
    
<input id="Button1"  onclick="validate();" type="button" value="確定" /> 
</form>
</body>
</html>

canry Tong 2012-03-16 08:58 發(fā)表評論
]]>
主站蜘蛛池模板: 砀山县| 休宁县| 化隆| 图们市| 常熟市| 湾仔区| 三门峡市| 抚顺市| 莱州市| 忻城县| 和顺县| 鄢陵县| 彰武县| 瓦房店市| 安新县| 太湖县| 慈溪市| 东宁县| 二手房| 鲜城| 安宁市| 黄平县| 平邑县| 临高县| 秭归县| 晋州市| 铜川市| 伊春市| 盘山县| 武陟县| 辽阳市| 济宁市| 澄城县| 高邑县| 漠河县| 内江市| 威宁| 溧水县| 南京市| 桓台县| 文登市|