javaee添加验证码
package com.zh.code; import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random; import javax.imageio.ImageIO;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; public class CreateCode extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
response.setHeader("Content", "image/jpeg");
//7.禁止浏览器缓存 随机图片
response.setDateHeader("Expires", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache"); //1.创建一个图片的内在 bufferedImage
//BufferedImage image=new BufferedImage(80, 30, BufferedImage.TYPE_INT_ARGB);
BufferedImage image=new BufferedImage(50,30 , BufferedImage.TYPE_INT_BGR);
//
//2.得到image的画笔
Graphics g=image.getGraphics();
g.setColor(Color.CYAN);
g.fillRect(0, 0, 50, 30);
//在上面写数字
g.setColor(Color.BLACK);
g.setFont(new Font(null,Font.BOLD,20));
String num=this.getCodeNum();
g.drawString(num, 0, 20); //把得到的 验证码存在session中
request.getSession().setAttribute("code", num); //3.图片发送
ImageIO.write(image, "jpg", response.getOutputStream());
}
//返回的随机数据字符串
public String getCodeNum(){
Random randow=new Random();
String num=randow.nextInt(9999)+"";
StringBuffer stringBuffer=new StringBuffer("");
for(int i=0;i<4-num.length();i++){
stringBuffer.append("0");
}
num+=stringBuffer;
return num;
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
this.doGet(request, response); } }
javaee添加验证码的更多相关文章
- asp.net添加验证码
1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...
- PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...
- Angular企业级开发(9)-前后端分离之后添加验证码
1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...
- PHPCMS v9 自定义表单添加验证码
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- cas4.2.4 登添加验证码
看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 --> &l ...
- [phpcms v9]自定义表单添加验证码验证功能
修改 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...
- 【转】PHPCMS v9 自定义表单添加验证码验证
1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- cas添加验证码
cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...
随机推荐
- 怎样快速免费获取Windows版本的ZBrush
ZBrush是一款专业的3D绘制软件及数字雕刻软件,随着3D技术的不断进步,ZBrush也是越来越受到业内欢迎,在世界拥有了众多的粉丝和爱好者.相信很多用户对软件的体验就是从使用的版本开始的,本文就教 ...
- 怎么运用好ZBrush中Magnify膨胀笔刷
Magnify膨胀笔刷是ZBrush笔刷中经常使用的,利用该笔刷可绘制中心向四周膨胀的效果.本文内容向大家介绍ZBrush®中膨胀笔刷以便大家熟悉它的用法和特性. 查看更多内容请直接前往:http:/ ...
- Unity手机平台播放影片
播放视频方法 截止到目前的Unity4.2版本,要在手机平台上播放影片,有两种方法: 使用Unity自带的Move Texture http://docs.unity3d.com/Documentat ...
- Mobile Prototype Dev Res Collection(Unity原型开发资源储备)
资源储备 本文针对mobile原型开发阶段的资源收集 在做移动端的开发时,当有灵感想做些东西时,若是此时缺少美术资源和可用的脚本,此刻会有些纠结,今天在Assets Store上Mark了一些移动端开 ...
- android机型排行榜(201509)
http://forum.techweb.com.cn/thread-1352272-1-1.html
- 纯CSS3画出小黄人并实现动画效果
前言 前两天我刚发布了一篇CSS3实现小黄人动画的博客,但是实现的CSS3动画是基于我在站酷网找到的一张小黄人的jpg格式图片,并自己用PS抠出需要实现动画的部分,最后才完成的动画效果.但是,其实我的 ...
- 【转】【Asp.Net】了解使用 ASP.NET AJAX 进行局部页面更新
简介Microsoft的 ASP.NET 技术提供了一个面向对象.事件驱动的编程模型,并将其与已编译代码的优势结合起来.但其服务器端的处理模型仍存在技术本身所固有的几点不足: 进行页面更新需要往返服务 ...
- Wireshark命令行工具tshark
Wireshark命令行工具tshark 1.目的 写这篇博客的目的主要是为了方便查阅,使用wireshark可以分析数据包,可以通过编辑过滤表达式来达到对数据的分析:但我的需求是,怎么样把Data部 ...
- Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- 实战 SQL Server 2008 数据库误删除数据的恢复
SQL Server中误删除数据的恢复本来不是件难事,从事务日志恢复即可.但是,这个恢复需要有两个前提条件: 1. 至少有一个误删除之前的数据库完全备份. 2. 数据库的恢复模式(Recovery m ...