1. 程式人生 > >創建 驗證碼

創建 驗證碼

xtend enc pan pin 接受 檢測 on() name next

1.java代碼

 1 import java.awt.Color;
 2 import java.awt.Font;
 3 import java.awt.Graphics;
 4 import java.awt.image.BufferedImage;
 5 import java.io.IOException;
 6 import java.util.Random;
 7 import javax.imageio.ImageIO;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 public class ImageServlet extends HttpServlet{ 14 @Override 15 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 //通知瀏覽器不要緩存
17 resp.setHeader("Expires", "-1"); 18 resp.setHeader("Cache-Control", "no-cache"); 19 resp.setHeader("Prama", "no-cache"); 20 21 String code = req.getParameter("code"); //接受jsp頁面表單填入的驗證碼 22 int width=120; 23 int height=50; 24 //在內存中創建一幅圖 25
BufferedImage bu = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 26 //得到一個畫筆 27 Graphics gr = bu.getGraphics(); 28 //畫邊框 29 gr.setColor(Color.gray); 30 gr.drawRect(0, 0, width, height); 31 //背景 32 gr.setColor(Color.pink); 33 gr.fillRect(1, 1, width-2, height-2); 34 //幹擾線 35 Random ran = new Random(); 36 gr.setColor(Color.black); 37 for(int i=0;i<=10;i++){ 38 gr.drawLine(ran.nextInt(width), ran.nextInt(height), ran.nextInt(width), ran.nextInt(height)); 39 } 40 //驗證碼 41 gr.setColor(Color.red); 42 gr.setFont(new Font("宋體", Font.BOLD|Font.ITALIC, 35)); 43 int x =18; 44 String s =""; //接收隨機生成的驗證碼 45 for(int j=0;j<4;j++){ 46 s+=ran.nextInt(10)+""; 47 gr.drawString(s.substring(j,j+1), x, 35); 48 x+=25; 49 } 50 req.getSession().setAttribute("racode", s); // 51 //System.out.println(s); 52 if(code.equals(s)){ // 對比檢測前端與後端的驗證碼是否一致 53 System.out.println("驗證碼輸入正確"); 54 }else{ 55 System.out.println("驗證碼輸入錯誤"); 56 } 57 //輸出到網頁上 58 ImageIO.write(bu, "jpg", resp.getOutputStream()); 59 } 60 @Override 61 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 62 this.doGet(req, resp); 63 } 64 }

2.jsp代碼

 1 <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html>
 3 <html>
 4     <head>
 5         <title>驗證碼</title>
 6         <script type="text/javascript">
 7             function tofresh(){
 8                 document.location.href="ImageLogin.jsp";
 9             }
10         
11         </script>
12     </head>
13     <body>
14         <form action="ImageServlet" method="post">
15             用戶名<input type="text" name="username"><br/>
16             密碼<input type="password" name="pass"><br/>
17             驗證碼<input type="text" name="code"><br/>
18             <img alt="驗證碼" src="/WEB/ImageServlet">
19             <a href="javascript:tofresh()">看不清,請再換一張</a>
20         </form>
21     </body>
22 </html>

3.配置web.xml 文件

1       <servlet>
2           <servlet-name>ImageServlet</servlet-name>
3           <servlet-class>ImageServlet</servlet-class>
4       </servlet>
5       <servlet-mapping>
6           <servlet-name>ImageServlet</servlet-name>
7           <url-pattern>/code</url-pattern>
8       </servlet-mapping> 

創建 驗證碼