• Java web验证码


    绘制验证码的主要步骤:

       1,设置宽度高度,验证码个数,干扰线个数,可选字符,背景颜色,字体格式

       2,画干扰线,随机生成颜色,字体,字符

       3,设置缓冲区,得到画笔,设置边框,读写数据,存储图片。

    1,Servlet方法:

      CheckCodeServlet类:

    package org.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.OutputStream;
    import java.util.Random;
    
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpSession;
    
    import sun.java2d.loops.DrawLine;
    
    /**
     * 输出随机的验证码
     */
    @WebServlet({ "/CheckCode", "/checkCode.jpg" })
    public class CheckCodeServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /* 宽度 */
        private final int WIDTH = 200;
        /* 高度 */
        private final int HEIGHT = 40;
        /* 生成验证码的个数 */
        private final int COUNT = 4;
        /* 干扰线条数 */
        private final int LINE_ROW = 6;
    
        /* 输出的基本码表,如果使用中文,则使用utf-8的码表,类似 ue234 ,而且应该使用常用字,避免出现偏僻字 */
        private final char[] BASECODE = { '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', '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' };
    
        // 写出数据
        private void write(HttpServletRequest request, HttpServletResponse response)
                throws IOException {
            HttpSession session = request.getSession();
    
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
                    BufferedImage.TYPE_INT_RGB);
            Graphics graphics = image.getGraphics();
    
            setBackground(graphics);
            drawBorder(graphics);
            drawDot(graphics);
            drawLine(graphics);
            drawString(graphics, session);
    
            // 写出数据流
            ImageIO.write(image, "jpg", response.getOutputStream());
    
        }
    
        // 写字
        private void drawString(Graphics graphics, HttpSession session) {
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
    
            graphics.setFont(new Font("宋体", Font.BOLD, 18));
            graphics.setColor(Color.BLACK);
    
            for (int i = 0; i < COUNT; i++) {
                String ch = String
                        .valueOf(BASECODE[random.nextInt(BASECODE.length)]);
                sb.append(ch);
    
                // 设置位置
                int x = i * 20 + random.nextInt(12) + 10;
                int y = random.nextInt(HEIGHT / 3) + 12;
    
                // 旋转字体
                double theta = Math.PI / 180 * random.nextInt(20);
                // rotate(graphics, theta);
    
                graphics.drawString(ch, x, y);
    
                // 恢复。。
                // rotate(graphics, -theta);
            }
            session.setAttribute("checkCode", sb.toString());
    
            System.out.println("   验证码:" + sb.toString());
        }
    
        // 旋转
        private void rotate(Graphics graphics, double theta) {
            ((Graphics2D) graphics).rotate(theta);
        }
    
        // 画随机线条
        private void drawLine(Graphics graphics) {
            Random random = new Random();
            for (int i = 0; i < LINE_ROW; i++) {
                int x1 = random.nextInt(WIDTH);
                int y1 = random.nextInt(HEIGHT);
                int x2 = random.nextInt(WIDTH);
                int y2 = random.nextInt(HEIGHT);
                setRandomColor(graphics);
                graphics.drawLine(x1, y1, x2, y2);
            }
        }
    
        // 画斑点
        private void drawDot(Graphics graphics) {
            Random random = new Random();
            graphics.setColor(Color.red);
            for (int i = 0; i < WIDTH; i++) {
                int x = i;
                int y = random.nextInt(HEIGHT);
                int r = random.nextInt(2);
                // graphics.fillOval(x, y, r, r);
                graphics.drawOval(x, y, r, r);
            }
        }
    
        // 画边框
        private void drawBorder(Graphics graphics) {
            graphics.setColor(Color.BLACK);
            graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);
        }
    
        // 设置背景
        private void setBackground(Graphics graphics) {
            graphics.setColor(Color.WHITE);
            graphics.fillRect(0, 0, WIDTH, HEIGHT);// 填充背景色
        }
    
        // 设置随机的画笔颜色
        private void setRandomColor(Graphics g) {
            Random random = new Random();
            g.setColor(new Color(random.nextInt(255), random.nextInt(255), random
                    .nextInt(255)));
        }
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
    
            // 输出图片流的头信息
            response.setContentType("image/jpeg");
            response.setHeader("Expires", "-1");
            response.setHeader("Cache-Control", "no-cache");
    
            // 写出数据
            write(request, response);
    
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
        }
    
    }
      
    

    2,XML文件配置:

        将CheckCodeServlet写入配置。

    3,jsp文件显示验证码:

        

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <script type="text/javascript">
            function flushCheckCode(obj) {
                obj.src = (obj.src + '?' + new Date())
            }
        </script>
    </head>
    <body>
    	<form action="LoginServlet" method="post" align="center">  
    	<table align="center">
    	    <caption><p class="text1">用户登陆</p></caption>
    	       <tr>
    	         <td>
    	                           用户名:
    	         </td>
    	         <td>
    	           <input name="username" type="text" placeholder="Userneme">
    	         </td>
    	       </tr>
    	       <tr>
    	        <td>
    	                            密 码:
    	        </td>
    	        <td>
    	           <input name="password" type="password"  placeholder="Password">
    	        </td>
    	       </tr>
    	       <tr>
    	       <td>
    	                       验证码:
    	       </td>
    	       <td>
               <input type="text" name="checkCode" placeholder="CheckCode"/>
               </td>
               </tr>
               </table>
               <tr>
               <td>
                              
               <img src="checkCode.jpg" onclick=flushCheckCode(this) alt="点击刷新验证码" />
    	       </td>
    	       </tr>
    	       <br/>
    	       <input name="submit" type="submit" class="button" value="登陆"/></br>
    	       <p class="text2"><a href="register.jsp">没有账号?来注册一个吧!</a></font></p>
    	</form>
    </body>
    </html>
    

      

  • 相关阅读:
    强制设置IE浏览器的版本模式
    设置DIV根据内容自动调整高度的三个方法
    VS2010如何调试IIS上的网站
    DataSet和List<T> 泛型之间互相转换 (转载, 作者写的很好)
    List转DataSet
    数据库中单个表数据备份
    第二阶段冲刺(第二天)
    冲刺第二阶段(第一天)
    学习进度条(第十四周)
    学习进度条(第十三周)
  • 原文地址:https://www.cnblogs.com/HPioneer/p/6531011.html
Copyright © 2020-2023  润新知