验证码-简单实现
验证码:
创建缓存图片:指定宽width=90,height=30
获取画笔对象
设置画笔颜色
填充矩形区域
从字符数组中随机得到字符 char[] arr = { ‘A’, ‘B’, ‘C’, ‘D’, ‘N’, ‘E’, ‘W’, ‘b’, ‘o’, ‘y’, ‘1’, ‘2’, ‘3’, ‘4’,‘5’,‘6’ };
循环4次,画4个字符
设置字的颜色为随机
设置字体,大小为18,
将每个字符画到图片,x增加,y不变。10+(i*20), 20
线的位置是随机的,x范围在width之中,y的范围在height之中。
画8条干扰线,每条线的颜色不同
将缓存的图片输出到响应输出流中
验证码Servlet代码
@WebServlet(name = "PicCodeServlet", urlPatterns = "/code")
public class PicCodeServlet extends HttpServlet {
//创建一个随机类 private Random ran = new Random(); //写一个方法随机生成一种颜色 private Color getRandomColor() { //随机生成0~255之间的数 int red = ran.nextInt(256); int green = ran.nextInt(256); int blue = ran.nextInt(256); //红,绿,蓝 return new Color(red, green, blue); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1. 创建缓存图片 int width = 90, height = 30; //参数:宽,高,图片模式 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //2. 获取画笔对象 Graphics graphics = img.getGraphics(); //3. 设置画笔颜色 graphics.setColor(Color.WHITE); //4. 填充矩形区域 graphics.fillRect(0, 0, width, height); //5. 从字符数组中随机得到字符 char[] arr = { 'A', 'B', 'C', 'D', 'N', 'E', 'W', 'b', 'o', 'y', '1', '2', '3', '4','5','6' }; //6. 循环4次,画4个字符 for (int i = 0; i < 4; i++) { //7. 设置字的颜色为随机 graphics.setColor(getRandomColor()); //8. 设置字体,大小为18 graphics.setFont(new Font(Font.SANS_SERIF,Font.BOLD+Font.ITALIC,19)); //随机得到下标 int index = ran.nextInt(arr.length); char c = arr[index]; //随机得到一个字符 //9. 将每个字符画到图片,x增加,y不变。 graphics.drawString(String.valueOf(c),10+(i*20), 20); } //11. 画8条干扰线,每条线的颜色不同 for (int i = 0; i < 8; i++) { //10. 线的位置是随机的,x范围在width之中,y的范围在height之中。 int x1 = ran.nextInt(width); int y1 = ran.nextInt(height); int x2 = ran.nextInt(width); int y2 = ran.nextInt(height); graphics.setColor(getRandomColor()); graphics.drawLine(x1,y1,x2,y2); } //12. 将缓存的图片输出到响应输出流中 //参数:图片对象,图片格式,输出流 ImageIO.write(img,"jpg",response.getOutputStream()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }
}
登录是用来校验效果怎么样,你们也可以直接拿到自己的项目里试试效果,这个登录页面随便找的一个校验的,页面效果比较丑,还有些样式没有导进来。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>登录页面</title> <link href="css/bootstrap.min.css" rel="stylesheet"> <script src="js/jquery-3.2.1.min.js"></script> <script src="js/bootstrap.min.js"></script> </head> <body> <div class="container" style="max-width:400px"> <h3 style="text-align: center">用户登录</h3> <form action="login" method="post" > <div class="form-group"> <label for="name">用户名:</label> <input type="text" name="name" class="form-control" id="name" placeholder="请输入用户名"> </div> <div class="form-group"> <label for="password">密码:</label> <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/> </div> <div class="form-inline"> <label for="vcode">验证码:</label> <input type="text" name="vcode" class="form-control" id="vcode" placeholder="验证码" style="width: 70px" maxlength="4"/> <!--验证码--> <img id="imgCode" src="code" style="width: 90px; height: 30px; cursor: pointer;" title="看不清,点击刷新"> <script type="text/javascript"> //图片点击事件 document.getElementById("imgCode").onclick = function () { this.src = "code?t=" + new Date().getTime(); }; </script> </div> <div style="text-align: center; padding-top: 20px;"> <input type="submit" value=" 登 录 " class="btn btn-primary"/> </div> </form> </div> </body> </html>
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Python3入门(十四)正则表达式
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。Python自1.5版本起增加了re模块,它提供Perl风格的正则表达式模式。re模块使Python语言拥有全部的正则表达式功能。compile函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象。该对象拥有一系列方法用于正则表达式匹配和替换。re模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数。 本文将给大家介绍Python中常用的正则表达式处理函数 一、正则表达式修饰符-可选标志 正则表达式可以包含一些可选标志修饰符来控制匹配的模式。修饰符被指定为一个可选的标志。多个标志可以通过按位OR(|)它们来指定。如re.I | re.M被设置成I和M标志 修饰符 描述 re.I 使匹配对大小写不敏感 re.L 做本地
- 下一篇
如何运用PHP+REDIS解决负载均衡后的session共享问题
一、为什么要使用Session共享? 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名、密码在整个网站的各个模块中都是可以登录使用的。各个服务器共享用户数据是比较容易实现的,只需要在后端放个数据库服务器,各个服务器通过统一接口对用户数据进行访问即可。但还存在一个问题,就是用户在这个服务器登录之后,进入另一个服务器的别的模块时,仍然需要重新登录,这就是一次登录,全部通行的问题,映射到技术上,其实就是各个服务器之间如何实现共享 SESSION 数据的问题。 二、了解session工作原理 在解决问题之前,先来了解一下 PHP SESSION 的工作原理。在客户端(如浏览器)登录网站时,被访问的 PHP 页面可以使用 session_start()
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- MySQL8.0.19开启GTID主从同步CentOS8
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS6,CentOS7官方镜像安装Oracle11G