java web中验证码的实现
生活随笔
收集整理的這篇文章主要介紹了
java web中验证码的实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們知道在我們注冊用戶的時候一般都有一段模糊的驗證碼讓我們輸入,其實我們自己也可以實現(xiàn)這個驗證碼 ,驗證碼的好處是為了防止某些自動提交軟件的而已行為? 。
下面就利用Servlet +JSP+JavaBran實現(xiàn)一個驗證碼機制。
BufferedImage? 可以操作緩沖區(qū)的 內(nèi)部Image,可以被ImageIO輸出到輸出流中 ,我們就是利用PrintWriter可以想瀏覽器輸出信息的原理我們輸出Image圖片 。
?
1、產(chǎn)生驗證碼的類 MakePicture
package me.test; import java.awt.Graphics; import java.awt.Font; import java.awt.Color; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.util.Random; import java.io.OutputStream; import java.io.IOException; public class MakePicture //產(chǎn)生識別驗證圖像 { private char charTable[]={'a','A','b','B','c','C','d','D' ,'e','E' ,'f','F','g','G','h','H','i','I','j','J' ,'0','1','2','3','4','5','6','7','8','9'}; public String drawPicture(int width,int height,OutputStream os){if(width<=0)width=100 ;if(height<=0) height=60 ;BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB) ;Graphics g=image.getGraphics() ;g.setColor(Color.LIGHT_GRAY) ; g.fillRect(0, 0, width, height) ;g.setColor(new Color(0x5265fd)) ;g.drawRect(0, 0, width, height) ; String str ="" ;for(int x=0;x<4;x++){str+=charTable[(int) (Math.random()*charTable.length)];}g.drawString(str.substring(0, 1), 0, 15);g.drawString(str.substring(1, 2), 15, 17);g.drawString(str.substring(2, 3), 35, 19);g.drawString(str.substring(3, 4), 50, 16);Random rand=new Random() ;for(int i=0;i<10;i++){int x=rand.nextInt(width) ;int y=rand.nextInt(height) ; g.drawOval(x, y, 1, 1) ;}g.dispose() ;try {ImageIO.write(image, "JPEG",os) ;} catch (IOException e) {e.printStackTrace();return "" ;}return str ;}}2、index.jsp首頁? 這個頁面通過請求Servlet輸出驗證碼
<%@ page language="java" contentType="text/html; charset=gb2312"pageEncoding="gb2312"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>圖片驗證</title> </head> <body><form action="yanzheng.jsp" method="get"> 驗證碼: <input type="text" name="code" /> <img src="show"><br> <input type="submit" value="提交"/> </form> </body> </html>
3、yanzheng.jsp 驗證輸入的字符是否正確
4、實現(xiàn)http請求的Servlet實現(xiàn)類? ImageServlet??
package me.test; import java.io.IOException; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public class ImageServlet extends HttpServlet { @Overrideprotected void service(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {MakePicture mp=new MakePicture() ;String str=mp.drawPicture(60, 20,res.getOutputStream() ) ;req.getSession().setAttribute("pic", str) ;res.getOutputStream().print(str) ;}}?
轉載于:https://www.cnblogs.com/yuedongwei/archive/2012/05/05/4145483.html
總結
以上是生活随笔為你收集整理的java web中验证码的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常见的加密算法 (转自: http://
- 下一篇: 将Apache添加为Linux的服务 实