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%的时间),而不是鼓动大家什么都去学最后什么都学不精,更不是说. ...
随机推荐
- VirtualBox 使用技巧
一.VirtualBox 的快捷键 VirtualBox 默认的 Host 键是 Right Ctrl Host 键可以点击 Oracle VM VirtualBox 管理器的左上角 “管理”-> ...
- Qt调用Server SQL中的存储过程
Server SQL中的存储过程如下: CREATE procedure PINSERTPC @pcnum int, @pcname varchar(50), @pctype int, @ipaddr ...
- Linq&Lumbda (2)
"Lambda表达式"是一个匿名函数,它可以包含表达式和语句,并且可用于创建委托或表达式树类型. Lambda 运算符: => 该运算符读为"goes to&quo ...
- A+Bproblem
package A+Bproblem; /* * A+B Problem 时间限制:3000 ms | 内存限制:65535 KB 难度:0 描述 此题为练手用题,请大家计算一下a+b的值 输入 ...
- [LeetCode] Combination Sum (bfs)
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- JS之tagNaem和nodeName
nodeName是节点的属性,tagName是元素的属性.元素是节点的子集.不是任何节点都有tagName的,比如文本节点,仅有nodeName属性. 这个和css中的倾斜和斜体的关系是一样的.不是所 ...
- WPF数据库连接错误:The user is not associated with a trusted SQL Server connection.
我当初安装sql server的时候选的Window Authentication mode,没选SQL Server Windows Authentication. 后来做WPF时连接数据库时需要一 ...
- [代码片段]YEAH!连通域标记和计数
//标记的连通域存储在buff[]里 //返回值为连通域个数 int LinkBlob(unsigned char **imagedata,unsigned char buff[], int heig ...
- SQL server 2012
MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28MICROSOFT SQL SERVER 2012 商业智能版激 ...
- LintCode Kth Largest Element
原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/# 在LeetCode上也有一道,采用了标准的quickSelect 方法 ...