• jsp验证码 (通过单击验证码或超链接换验证码)


    #code.jsp
    
    <%@ page language="java"
        import="java.util.*"
        import="java.awt.*"
        import="java.awt.image.BufferedImage"
        import="javax.imageio.ImageIO"
        pageEncoding="UTF-8"
    %>
    <%
        response.setHeader("Cache-Control","no-cache");
        int width = 60;
        int height = 20;
        int length = 4;
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //设置画笔
        Graphics g = image.getGraphics();
        //设定背景颜色
        g.setColor(new Color(200,200,200));
        g.fillRect(0, 0, width, height);
        //取随机产生的验证码(四位数字)
        
        String randStr = "";  
        Random random = new Random();  
        for (int i = 0; i < length; i++) {  
            boolean b = random.nextBoolean();  
            if (b) { // 字符串  
                int choice = random.nextBoolean() ? 65 : 97; //取得65大写字母还是97小写字母  
                randStr += (char) (choice + random.nextInt(26));// 取得大写字母  
            } else { // 数字  
                randStr += String.valueOf(random.nextInt(10));  
            }  
        }  
        session.setAttribute("randStr", randStr);
        //将验证码显示到图像中
        g.setColor(Color.black);
        g.setFont(new Font("",Font.PLAIN,20));
        g.drawString(randStr,5,17);
        //随机产生100个干扰点,使图像不容易被其它程序探测
        for(int i=0; i<100; i++){
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.drawOval(x,y,1,1);
        }
        //输出图像的到页面
        ImageIO.write(image,"JPEG",response.getOutputStream());
        out.clear();
        out = pageContext.pushBody();
    %>
    
    
    # loginForm.jsp
    
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <html>
        <body>
            welcome
            <br>
            <form name="loginForm" method="post" action="/Pro27/servlet/CheckCode">
                account:<input type="text" name="account"><br>
                password:<input type="password" name="password"><br>
                code:<input type="text" size="10" name="code">
                <img name="imgValidate" src="code.jsp" onClick="refresh()">
                <a href="javascript:refresh();">看不清,换一张 </a>  
                <br>
                <input type="submit" value="login">
                <br>
                ${sessionScope.codeCheck }
                <%session.removeAttribute("codeCheck"); %>
            </form>
        </body>
        <script type="text/javascript" language="JavaScript">
            function refresh(){
                loginForm.imgValidate.src = "code.jsp?"+Math.random();//new Date(); 此处必须加上Math.random()或new Date(),否则无法刷新验证码
            }
        </script>
    </html>
    
  • 相关阅读:
    Object之克隆对象clone 和__clone()函数
    Object之魔术函数__toString() 直接输出对象引用时自动调用
    Object之魔术函数__call() 处理错误调用
    Git关联远程GitHub仓库
    python制作查找单词翻译的脚本
    用python处理文本,本地文件系统以及使用数据库的知识基础
    基于序列化技术(Protobuf)的socket文件传输
    Python核心编程——Chapter16
    gdb初步窥探
    unp学习笔记——Chapter1
  • 原文地址:https://www.cnblogs.com/jxgapyw/p/5045926.html
Copyright © 2020-2023  润新知