【Servlet】实现简单的验证码生成与验证
2015-09-19 09:02
519 查看
简单的四位数字验证码生成,带看不清刷新验证密码的功能
登陆界面:
验证码:<input type="text" name="loginCode" id="loginCode" />
<img src="code.do" id="codeImage" style="width:80px;height:20px">
<input type="button" id = "getCode" value="看不清" onclick="button_onclick()" />
<br/>
//设置了图片大小,添加了刷新图片的button
JavaScript:
<script type="text/javascript">
function button_onclick(){
var src = "code.do/" + Math.random();
document.getElementById("codeImage").src = src;
}
</script>
//实现图片刷新功能,通过添加随机数生成不同的请求
web.xml:servlet-mapping这样设置
<servlet-mapping>
<servlet-name>code</servlet-name>
<url-pattern>/code.do/*</url-pattern>
</servlet-mapping>
生成验证码的Servlet:
public class Code extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
int h = 20;
int w = 80;
g.fillRect(0, 0, w, h);
g.setColor(Color.BLUE);
g.drawRect(1, 1, 78, 18);
String code = "";
Random r = new Random();
for(int i = 0; i < 4; i++){
int a = r.nextInt(9);
code += a;
}
HttpSession hs = request.getSession();
hs.setAttribute("code", code);//将随机生成的4位验证码存入会话
g.setColor(RandomColor.randomColor());
g.drawString(code, 25, 15);
//随机十条线
for(int i = 0; i < 10; i++){
int x = r.nextInt(w);
int y = r.nextInt(h);
int x1 = r.nextInt(12);
int y1 = r.nextInt(12);
g.setColor(RandomColor.randomColor());
g.drawLine(x, y, x + x1, y + y1);
}
//设置网页立刻过期
response.setDateHeader( "expries" , -1 );
response.setHeader("cache-control" , "no-cache");
response.setHeader("Pragma" , "no-cache");
//将image写给浏览器
ImageIO.write( image , "jpg" , response.getOutputStream() );
}
}
生成随机颜色的辅助类:
public class RandomColor {
public static Color randomColor(){
int r = 0;
int b = 0;
int g = 0;
Random random = new Random();
r = random.nextInt(255);
g = random.nextInt(255);
b = random.nextInt(255);
Color c = new Color(r,g,b);
return c;
}
}
进行验证的Servlet:
String code = request.getParameter("loginCode");//取JSP中的 code
code.equals(request.getSession().getAttribute("code"))//与session中的code比较
登陆界面:
验证码:<input type="text" name="loginCode" id="loginCode" />
<img src="code.do" id="codeImage" style="width:80px;height:20px">
<input type="button" id = "getCode" value="看不清" onclick="button_onclick()" />
<br/>
//设置了图片大小,添加了刷新图片的button
JavaScript:
<script type="text/javascript">
function button_onclick(){
var src = "code.do/" + Math.random();
document.getElementById("codeImage").src = src;
}
</script>
//实现图片刷新功能,通过添加随机数生成不同的请求
web.xml:servlet-mapping这样设置
<servlet-mapping>
<servlet-name>code</servlet-name>
<url-pattern>/code.do/*</url-pattern>
</servlet-mapping>
生成验证码的Servlet:
public class Code extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.BLACK);
int h = 20;
int w = 80;
g.fillRect(0, 0, w, h);
g.setColor(Color.BLUE);
g.drawRect(1, 1, 78, 18);
String code = "";
Random r = new Random();
for(int i = 0; i < 4; i++){
int a = r.nextInt(9);
code += a;
}
HttpSession hs = request.getSession();
hs.setAttribute("code", code);//将随机生成的4位验证码存入会话
g.setColor(RandomColor.randomColor());
g.drawString(code, 25, 15);
//随机十条线
for(int i = 0; i < 10; i++){
int x = r.nextInt(w);
int y = r.nextInt(h);
int x1 = r.nextInt(12);
int y1 = r.nextInt(12);
g.setColor(RandomColor.randomColor());
g.drawLine(x, y, x + x1, y + y1);
}
//设置网页立刻过期
response.setDateHeader( "expries" , -1 );
response.setHeader("cache-control" , "no-cache");
response.setHeader("Pragma" , "no-cache");
//将image写给浏览器
ImageIO.write( image , "jpg" , response.getOutputStream() );
}
}
生成随机颜色的辅助类:
public class RandomColor {
public static Color randomColor(){
int r = 0;
int b = 0;
int g = 0;
Random random = new Random();
r = random.nextInt(255);
g = random.nextInt(255);
b = random.nextInt(255);
Color c = new Color(r,g,b);
return c;
}
}
进行验证的Servlet:
String code = request.getParameter("loginCode");//取JSP中的 code
code.equals(request.getSession().getAttribute("code"))//与session中的code比较
相关文章推荐
- 何如在ADT中使用genymotion进行测试
- Vector和ArrayList比较
- Android 关于finish()、onDestory()、System.exit(0)、onCheckedChanged(RadioGroup arg0, int checkId)总结
- 使用Sax和Pull方法 解析XML文件
- 浣溪沙。花与果
- 如何获得最近的餐馆谁
- 汽车租赁系统
- 一个类型转换的讨论
- IT未来发展前景
- Android——单、多线程下载
- mysql 常用查询
- 一些变量的比较
- USACO 1.3 Combination Lock
- POJ——1323 Game Prediction
- "_OBJC_CLASS_$_MAMapServices", referenced from: 的问题修复
- TCP和UDP比较
- 架构设计:系统间通信(3)——IO通信模型和JAVA实践 上篇
- 自学安卓复习基础_之四(关于intent和bundle传值)
- 大道至简第一章感悟上
- 万能搜索