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添加验证码的更多相关文章

  1. asp.net添加验证码

    1.新建一个aspx页面生成验证码图像 using System; using System.Data; using System.Configuration; using System.Collec ...

  2. PHPCMS v9 自定义表单添加验证码验证

    1. 在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  3. Angular企业级开发(9)-前后端分离之后添加验证码

    1.背景介绍 团队开发的项目,前端基于Bootstrap+AngularJS,后端Spring MVC以RESTful接口给前端调用.开发和部署都是前后端分离.项目简单部署图如下,因为后台同时采用微服 ...

  4. PHPCMS v9 自定义表单添加验证码

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  5. cas4.2.4 登添加验证码

    看了很多添加验证码的博文,唯独没有4.24的 重点看第3条,其余的和别人博文大致相同 1.首先在cas工程的web.xml增加验证码功能的支持 <!-- 验证码功能 -->      &l ...

  6. [phpcms v9]自定义表单添加验证码验证功能

    修改  \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=" ...

  7. 【转】PHPCMS v9 自定义表单添加验证码验证

    1.  在 \phpcms\templates\default\formguide\show.html 中添加验证码显示 <input type="text" id=&quo ...

  8. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  9. cas添加验证码

    cas添加验证码,折腾了好久,终于整理好了,很大部分都是借鉴http://binghejinjun.iteye.com/blog/1255293这个的.但是他的有一个很不好的地方就是不能提升验证码错误 ...

随机推荐

  1. 如何判断两个String是否是Anagrams_java实现

    Anagrams:是颠倒字母顺序的字符串 本文提供三个方法,分别分析时间空间复杂度 方法一:暴力遍历 时间复杂度:O(n^2) 方法二:基于排序算法,Sorting的时间复杂度是O(n*log(n)) ...

  2. python中*args和**args的不同

    上一段代码,大家感受一下 def test_param(*args): print(args) def test_param2(**args): print(args) test_param('tes ...

  3. 04JavaIO详解_DataInputStream(属于过滤l流)

    DataInputStream这个类是二进制读写的.并且 是过滤流,会一层套一层的.这里就是装饰者模式. public class DataStream1 { public static void m ...

  4. 第四章 在MVC4.0中对脚本以及样式表的引用变化

    原文:http://www.cnblogs.com/xdotnet/archive/2012/07/21/aspnet40_webpage20.html 一.可以直接使用“~”,而无需使用Href对象 ...

  5. Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别

    Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...

  6. C#基础系列:实现自己的ORM(反射以及Attribute在ORM中的应用)

    反射以及Attribute在ORM中的应用 一. 反射什么是反射?简单点吧,反射就是在运行时动态获取对象信息的方法,比如运行时知道对象有哪些属性,方法,委托等等等等.反射有什么用呢?反射不但让你在运行 ...

  7. web前端性能意义、关注重点、测试方案、优化技巧

    1.前段性能的意义 对于访问一个网站,最花费时间的并不是后端应用程序处理以及数据库等消耗的时间,而是前端花费的时间(包括请求.网络传输.页面加载.渲染等).根据web优化的黄金法则: 80%的最终用户 ...

  8. 淘宝账号基于OAuth2.0的登录验证授权登陆第三方网站

    首先得有一个注册的appkey和App Secret   该流程分三个步骤: 第一步:通过用户授权获取授权码Code: 第二步:用上一步获取的Code和应用密钥(AppSecret)通过Https P ...

  9. pandas 给数据打标签

    import numpy as np import pandas as pd df = pd.DataFrame(np.random.randint(0,100,100), columns=['sco ...

  10. pandas 时间序列resample

    resample与groupby的区别:resample:在给定的时间单位内重取样groupby:对给定的数据条目进行统计 函数原型:DataFrame.resample(rule, how=None ...