五月综合缴情婷婷六月,色94色欧美sute亚洲线路二,日韩制服国产精品一区,色噜噜一区二区三区,香港三级午夜理伦三级三

您現(xiàn)在的位置: 365建站網(wǎng) > 365文章 > ImageServlet實現(xiàn)驗證碼

ImageServlet實現(xiàn)驗證碼

文章來源:365jz.com     點擊數(shù):366    更新時間:2009-11-09 22:40   參與評論
 

1.ImageServlet類的實現(xiàn):

package com.mobile.control;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {

       public void doGet(HttpServletRequest request, HttpServletResponse response)

                     throws ServletException, IOException {

              this.doPost(request, response);

       }

       // 生成數(shù)字和字母的驗證碼

       public void doPost(HttpServletRequest request, HttpServletResponse response)

                     throws ServletException, IOException {

              BufferedImage img = new BufferedImage(68, 22,

                            BufferedImage.TYPE_INT_RGB);

              // 得到該圖片的繪圖對象

              Graphics g = img.getGraphics();

              Random r = new Random();

              Color c = new Color(200, 150, 255);

              g.setColor(c);

              // 填充整個圖片的顏色

              g.fillRect(0, 0, 68, 22);

              // 向圖片中輸出數(shù)字和字母

              StringBuffer sb = new StringBuffer();

              char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

              int index, len = ch.length;

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

                     index = r.nextInt(len);

                     g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt

(255)));

                     g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));// 輸出的

字體和大小                 

                     g.drawString("" + ch[index], (i * 15) + 3, 18);//寫什么數(shù)字,在圖片

的什么位置畫

                     sb.append(ch[index]);

              }

              request.getSession().setAttribute("piccode", sb.toString());

              ImageIO.write(img, "JPG", response.getOutputStream());

       }    

}

2.web.xml中的配置

       <servlet>

              <servlet-name>ImageServlet</servlet-name>

              <servlet-class>com.mobile.control.ImageServlet</servlet-class>

       </servlet>

       <servlet-mapping>

              <servlet-name>ImageServlet</servlet-name>

              <url-pattern>/servlet/ImageServlet</url-pattern>

       </servlet-mapping>

3.頁面源程序

<%@ page contentType="text/html; charset=utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

       <head>

              <title>系統(tǒng)登錄</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 href="<%=request.getContextPath()%>/jsp/style/stylet.css"

                     rel="stylesheet" type="text/css">  

              <mce:script type="text/javascript"><!--

                     // 重載驗證碼

                     function reloadVerifyCode(){

                            var timenow = new Date().getTime();                       

                            document.getElementById("safecode").src="<%

=request.getContextPath()%>/servlet/ImageServlet?d="+timenow;

                     }

// --></mce:script>

       </head>

       <body>

              <div id="box1">

                     <div id="tab_box">

                            <form action="<%=request.getContextPath() %

>/servlet/LoginServlet" method="post" name="login">

                                   <span class="denglu_font_title">歡迎登錄</span>

                                   <br />

                                   <br />

                                   <label for="userid" class="yonghuming_font">

                                          用戶:

                                   </label>

                                   <input name="userid" type="text" id="userid"

accesskey="n"

                                          tabindex="1"

                                          style="font-family: '宋體'; font-size: 12px;

color: #575757" />

                                   <br />

                                   <br />

                                   <label for="passwd" class="yonghuming_font">

                                          密碼:

                                   </label>

                                   <input name="passwd" type="password" id="passwd"

tabindex="2"

                                          style="font-family: '宋體'; font-size: 12px;

color: #575757" />

                                   <br />

                                   <br />                                              

                                   <label for="verifyCode" class="yonghuming_font">

                                          驗證碼:

                                   </label>

                                   <input type="text" id="verifyCode" name="verifyCode"

size="6" />

                                   <img alt="驗證碼" id="safecode" src="<%

=request.getContextPath()%>/servlet/ImageServlet">

                                   <a href="javascript:reloadVerifyCode();"

mce_href="javascript:reloadVerifyCode();">看不清楚</a>

                                   <input name="denglu_tijiao" type="submit"

id="denglu_tijiao"

                                          tabindex="3" value="確認" />

                                   <input name="denglu_chongzi" type="reset"

id="denglu_reset"

                                          tabindex="4" value="重置" />

                                   <input type="hidden" name="flag" value="logon">

                            </form>

                            <font color='red'></font>

                     </div>

              </div>

       </body>

</html>

如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答??! 點擊進入論壇

發(fā)表評論 (366人查看0條評論)
請自覺遵守互聯(lián)網(wǎng)相關(guān)的政策法規(guī),嚴禁發(fā)布色情、暴力、反動的言論。
昵稱:
最新評論
------分隔線----------------------------

其它欄目

· 建站教程
· 365學(xué)習(xí)

業(yè)務(wù)咨詢

· 技術(shù)支持
· 服務(wù)時間:9:00-18:00
365建站網(wǎng)二維碼

Powered by 365建站網(wǎng) RSS地圖 HTML地圖

copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號