生成验证码
来源:互联网 发布:950 600淘宝轮播图 编辑:程序博客网 时间:2024/06/05 19:41
生成验证码
package com.itheima.servlet;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;/** * 验证码 * @author thinkpad * */public class servletDemo4 extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width=110; int height=25; int imageType=BufferedImage.TYPE_INT_RGB; //在内存中创建一个图像对象 BufferedImage img=new BufferedImage(width, height, imageType); //创建画笔 Graphics g =img.getGraphics(); //给图片添加背景颜色 g.setColor(Color.PINK); g.fillRect(1, 1, width-2, height-2); //添加边框颜色 g.setColor(Color.RED); g.drawRect(0, 0, width-1, height-1); //添加文字 g.setFont(new Font("宋体", Font.BOLD|Font.ITALIC, 15)); g.setColor(Color.BLUE); Random rand=new Random(); int position=20; for (int i = 0; i < 4; i++) { g.drawString(rand.nextInt(10)+"", position, 18); position+=20; } //添加9跟干扰线 for (int i = 0; i < 9; i++) { g.drawLine(rand.nextInt(width), rand.nextInt(height), rand.nextInt(width), rand.nextInt(height)); } //将图片以对象方式输出到客户端 ImageIO.write(img, "jpg", response.getOutputStream()); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><script type="text/javascript"> function changeCode(){ var img=document.getElementsByTagName("img")[0]; img.src="/day05_1/demo2?time="+new Date().getTime(); }</script><title>Insert title here</title></head><body> <form action="#" method="post"> 用户名:<input type="text" name="userName"><br> 密码:<input type="password" name="password"><br> 验证码:<input type="text" name="code"> <img src="/day05_1/demo2" onclick="changeCode()"><a href="javascript:changeCode()">看不清,换一张</a><br> <input type="submit" value="提交"><br> </form></body></html>
阅读全文
1 0
- 验证码生成及验证
- 生成验证码并验证
- 验证码生成与验证
- C#生成验证码
- C#生成验证码
- 生成验证码
- 验证码生成
- 生成验证码
- JSP生成验证码
- 生成验证码
- 生成验证码
- 数字验证码生成
- 动态生成验证码
- 生成验证码(1)
- 生成验证码(2)
- c# 生成验证码
- 生成验证码图像
- 生成验证码
- 初试Jenkins2.0 Pipeline持续集成
- jdbc进行简单的增删改查
- 文章标题
- DirectX11 使用几何着色器实现公告板效果
- 【基础功能】c#写.csv文件
- 生成验证码
- 云盘小程序(Hbase数据库):注册,登录部分
- c#获取程序根目录方法
- 线程的实现
- Redis入门
- python 多维数组的排序
- ExcelUtil.java
- [ProtoBuf]ProtoBuf原理
- ExcelUtil.java