Servlet里写验证码
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里写验证码的更多相关文章
- 在php里写sql查询需要注意的事情
---恢复内容开始--- 今天往php里写了一条sql查询, $sql = "select * from videos where vuser=".$u: $ret = mysql ...
- Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致
Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null
- asp.net尽量不在js里写<%%>
asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...
- 前端要革命?看我在js里写SQL
在日新月异的前端领域中,前端工程师能做的事情越来越多,自从nodejs出现后,前端越来越有革了传统后端命的趋势,本文就再补一刀,详细解读如何在js代码中执行标准的SQL语句 为什么要在js里写SQL? ...
- servlet中生成验证码
在servlet中生成验证码 package login; import java.awt.Color; import java.awt.Graphics; import java.awt.image ...
- 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 ...
- java调c# exe 程序,exe里写文件问题
应用场景描述: java web程序,触发 调用c#写的后台exe程序,发现exe里写的文件找不到.单独在cmd命令行下执行exe没问题: 问题查找: 由于exe里获取文件路径错误导致: 解决方法: ...
- <ItemTemp>里写判断语句
<%@ Language="C#" %> <html> <head></head> <body> <%=DateT ...
- 不要困在自己建造的盒子里——写给.NET程序员(附精彩评论)
此文章的主旨是希望过于专注.NET程序员在做好工作.写好.NET程序的同时,能分拨出一点时间接触一下.NET之外的东西(例如10%-20%的时间),而不是鼓动大家什么都去学最后什么都学不精,更不是说. ...
随机推荐
- docker gitlab,redmine,etc development enviroments
sameersbn-gitlab is better more than officals sameersbn/redmine https://hub.docker.com/r/sameersbn/r ...
- 【android学习2】:Eclipse中HttpServlet类找不到
Eclipse中使用的HttpServlet类之所以识别不到的原因是没有导入Servlet-api.jar包,这个包在所安装在的tomcat的lib文件下,所以只需要导入即可. 在需要导入的工程上右键 ...
- java编程技巧
欢迎提出建议指出错误互相交流. 1.统计对象数量,比如统计一共发射了多少颗子弹. public class Bullet { public static int count = 0; public B ...
- 视频播放器开发中遇到的一些小问题MPMoviePlayerController
1 开发环境是 xcode6 ipad3真机 ios8.1.1越狱 需要添加以下代码 ,否则真机测试没有外音,只有耳机 NSError *setCategoryError = nil; ...
- (lleetcode)Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- django 笔记
最近开始接触django,一些基本的操作记录于此. 参考链接: http://www.ziqiangxuetang.com/django/django-tutorial.html django安装 s ...
- How to delete expired archive log files using rman?
he following commands will helpful to delete the expired archive log files using Oracle Recovery Man ...
- POJ2262问题描述
Goldbach's Conjecture Description In 1742, Christian Goldbach, a German amateur mathematician, sen ...
- linux多个python版本下导致no request报错
解决办法: sudo apt-get install uwsgi uwsgi-core uwsgi-plugin-python sudo apt-get install uwsgi-plugin-py ...
- Linux中硬件相关命令
http://www.cnblogs.com/cchust/p/3354570.html http://www.cnblogs.com/kerrycode/archive/2012/07/06/257 ...