public class CodeServlet extends HttpServlet {

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("image/jpeg");

// 创建图片缓冲流
int width = 90; // 宽度
int height = 25; // 高度
int imageType = BufferedImage.TYPE_INT_RGB; // 颜色
BufferedImage image = new BufferedImage(width, height, imageType);

// 获取缓冲图片流中的画布
Graphics g = image.getGraphics();
// 设置画笔的颜色
g.setColor(Color.lightGray);
// 填充画布区域
g.fillRect(0, 0, width, height);

// 输出的内容
String word = "abcdefhijklmnopqrstuvwxyz";
// 将要输出的内容,转换为 字节数组
char[] words = word.toCharArray();

// 定义一个随机对象
Random random = new Random();

String sessionWord = "";

for (int i = 0; i < 4; i++) {
String temp = words[random.nextInt(words.length)] + "";

sessionWord += temp;
// 更改画笔的颜色
g.setColor(new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255)));
g.setFont(new Font("微软雅黑", Font.BOLD, 20));
// 使用画笔,在画布上写字
g.drawString(temp, 20 * i + 10, 20);
}

request.getSession().setAttribute("code", sessionWord);

// 获取servlet 输出流
OutputStream output = response.getOutputStream();
// 图片io流
ImageIO.write(image, "jpeg", output);

}

}

Servlet里写验证码的更多相关文章

  1. 在php里写sql查询需要注意的事情

    ---恢复内容开始--- 今天往php里写了一条sql查询, $sql = "select * from videos where vuser=".$u: $ret = mysql ...

  2. Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致

    Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null

  3. asp.net尽量不在js里写<%%>

    asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...

  4. 前端要革命?看我在js里写SQL

    在日新月异的前端领域中,前端工程师能做的事情越来越多,自从nodejs出现后,前端越来越有革了传统后端命的趋势,本文就再补一刀,详细解读如何在js代码中执行标准的SQL语句 为什么要在js里写SQL? ...

  5. servlet中生成验证码

    在servlet中生成验证码 package login; import java.awt.Color; import java.awt.Graphics; import java.awt.image ...

  6. Asp.net mvc怎么在razor里写js代码

    我试图在Razor里写JS代码,但是不行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 3 ...

  7. java调c# exe 程序,exe里写文件问题

    应用场景描述: java web程序,触发 调用c#写的后台exe程序,发现exe里写的文件找不到.单独在cmd命令行下执行exe没问题: 问题查找: 由于exe里获取文件路径错误导致: 解决方法: ...

  8. <ItemTemp>里写判断语句

    <%@ Language="C#" %> <html> <head></head> <body> <%=DateT ...

  9. 不要困在自己建造的盒子里——写给.NET程序员(附精彩评论)

    此文章的主旨是希望过于专注.NET程序员在做好工作.写好.NET程序的同时,能分拨出一点时间接触一下.NET之外的东西(例如10%-20%的时间),而不是鼓动大家什么都去学最后什么都学不精,更不是说. ...

随机推荐

  1. UltraEdit 标签(tab)不见的3个解决办法

    UltraEdit 标签(tab)不见的3个解决办法 2010-11-08 09:19 1042人阅读 评论(0) 收藏 举报 工具c 方法1:点 视图->视图/列表(V)->打开文件标签 ...

  2. Liunx 下使用cmake

    参考 http://blog.chinaunix.net/uid-28458801-id-3501768.html http://www.ibm.com/developerworks/cn/linux ...

  3. UIView 的粗浅解析

    The UIView class defines a rectangular area on the screen and the interfaces for managing the conten ...

  4. 查询mysql当前连接数

    标签: mysql服务器cachedisk 2012-08-23 23:06 23377人阅读 评论(0) 收藏 举报  分类: MySql(36)  1.show status Threads_co ...

  5. VS2013修改MVC4默认生成的模板

    找到以下目录,根据VS版本和安装目录不同相应改动: I:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTempla ...

  6. Cocos2d-JS切换场景与切换特效

    var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { //////////////////////////// ...

  7. PHP运行错最有效解决办法Fatal error: Out of memory (allocated 786432) (tried to allocate 98304 bytes) in H:\freehost\zhengbao2\web\includes\lib_common.php on line 744

    原文 PHP运行错最有效解决办法Fatal error: Out of memory (allocated 6029312) Fatal error: Out of memory (allocated ...

  8. svn利用TortoiseSVN忽略文件或文件夹

    忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有 自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作. ...

  9. Face The Right Way---hdu3276(开关问题)

    题目链接:http://poj.org/problem?id=3276 题意:n牛头排成一排,每头牛两个状态,向前或向后,为了让所有的牛都向前,现在有一个机器 每次 能控制连续K头牛转换自己的状态,求 ...

  10. Selenium2学习-031-WebUI自动化实战实例-029-JavaScript 在 Selenium 自动化中的应用实例之四(获取元素位置和大小)

    通过 JS 或 JQuery 获取到元素后,通过 offsetLeft.offsetTop.offsetWidth.offsetHeight 即可获得元素的位置和大小,非常的简单,直接上源码了,敬请参 ...