JAVA验证码的实现参考

java验证码的实现(一) 

1.ImageServlet类的实现:

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);

       }

       // 生成数字和字母的验证码

       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);

              // 向图片中输出数字和字母

              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);//写什么数字,在图片

的什么位置画

                     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>系统登录</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>

你可能感兴趣的:(JavaScript,java,servlet,css,mobile)