当前位置:文档之家› javaWeb实现验证码

javaWeb实现验证码

java web中验证码的实现分类:JavaWeb编程2012-05-05 14:37 473人阅读评论(0) 收藏举报下面就利用Servlet +JSP+JavaBran实现一个验证码机制。

BufferedImage 可以操作缓冲区的内部Image,可以被ImageIO输出到输出流中,我们就是利用PrintWriter可以想浏览器输出信息的原理我们输出Image图片。

1、产生验证码的类MakePicture[java]view plaincopyprint?1.package me.test;2.import java.awt.Graphics;3.import java.awt.Font;4.import java.awt.Color;5.import java.awt.image.BufferedImage;6.import javax.imageio.ImageIO;7.import java.util.Random;8.import java.io.OutputStream;9.import java.io.IOException;10.public class MakePicture //产生识别验证图像11.{12. private char charTable[]={13. 'a','A','b','B','c','C','d','D' ,'e','E' ,14. 'f','F','g','G','h','H','i','I','j','J' ,15. '0','1','2','3','4','5','6','7','8','9'16. };17. public String drawPicture(int width,int height,OutputStream os)18. {19. if(width<=0)20. width=100 ;21. if(height<=0)22. height=60 ;23.24. BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB) ;25. Graphics g=image.getGraphics() ;26. g.setColor(Color.LIGHT_GRAY) ;27. g.fillRect(0, 0, width, height) ;28. g.setColor(new Color(0x5265fd)) ;29. g.drawRect(0, 0, width, height) ;30. String str ="" ;31. for(int x=0;x<4;x++)32. {33. str+=charTable[(int) (Math.random()*charTable.length)];34. }35.36. g.drawString(str.substring(0, 1), 0, 15);37. g.drawString(str.substring(1, 2), 15, 17);38. g.drawString(str.substring(2, 3), 35, 19);39. g.drawString(str.substring(3, 4), 50, 16);40. Random rand=new Random() ;41. for(int i=0;i<10;i++)42. {43. int x=rand.nextInt(width) ;44. int y=rand.nextInt(height) ;45. g.drawOval(x, y, 1, 1) ;46. }47. g.dispose() ;48. try {49. ImageIO.write(image, "JPEG",os) ;50. } catch (IOException e) {51.52. e.printStackTrace();53. return "" ;54. }55.56. return str ;57. }58.59.60.61.62.63.}2、index.jsp首页这个页面通过请求Servlet输出验证码[java]view plaincopyprint?1.<%@ page language="java" contentType="text/html; charset=gb2312"2. pageEncoding="gb2312"%>3.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" "http://ww/TR/html4/loose.dtd">4.<html>5.<head>6.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">7.<title>图片验证</title>8.</head>9.<body>10.11.<form action="yanzheng.jsp" method="get">12.验证码: <input type="text" name="code" />13.<img src="show"><br>14.<input type="submit" value="提交"/>15.</form>16.</body>17.</html>3、yanzheng.jsp 验证输入的字符是否正确[java]view plaincopyprint?1.<%@ page language="java" contentType="text/html; charset=ISO-8859-1"2. pageEncoding="ISO-8859-1"%>3.<!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" "http://ww/TR/html4/loose.dtd">4.<html>5.<head>6.<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">7.<title>Insert title here</title>8.</head>9.<body>10.<%11.12.out.print(request.getParameter("code")) ;13.14.%>15.</body>16.</html>4、实现http请求的Servlet实现类ImageServlet[java]view plaincopyprint?1.package me.test;2.import java.io.IOException;3.import javax.imageio.ImageIO;4.import javax.servlet.ServletException;5.import javax.servlet.annotation.WebServlet;6.import javax.servlet.http.HttpServlet;7.import javax.servlet.http.HttpServletRequest;8.import javax.servlet.http.HttpServletResponse;9.10.public class ImageServlet extends HttpServlet11.{12.@Override13. protected void service(HttpServletRequest req, HttpServletResponse res)14. throws ServletException, IOException {15. MakePicture mp=new MakePicture() ;16. String str=mp.drawPicture(60, 20,res.getOutputStream() ) ;17. req.getSession().setAttribute("pic", str) ;18. res.getOutputStream().print(str) ;19. }20.21.22.}。

相关主题