一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - java web驗(yàn)證碼實(shí)現(xiàn)代碼分享

java web驗(yàn)證碼實(shí)現(xiàn)代碼分享

2020-05-19 10:31沒頭沒腦沒煩惱 JAVA教程

這篇文章主要為大家分享了java web驗(yàn)證碼的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

驗(yàn)證碼的作用:通常的登錄或者注冊(cè)系統(tǒng)時(shí),都會(huì)要求用戶輸入驗(yàn)證碼,以此區(qū)別用戶行為和計(jì)算機(jī)程序行為,目的是有人防止惡意注冊(cè)、暴力破解密碼等。

實(shí)現(xiàn)驗(yàn)證碼的思路:用 server 實(shí)現(xiàn)隨機(jī)生成數(shù)字和字母組成圖片的功能,用 jsp 頁面實(shí)現(xiàn)顯示驗(yàn)證碼和用戶輸入驗(yàn)證碼的功能,再用 server 類分別獲取圖片和用戶輸入的數(shù)據(jù),判斷兩個(gè)數(shù)據(jù)是否一致。 
代碼實(shí)現(xiàn)

1.編寫數(shù)字、英文隨機(jī)生成的 server 類,源碼:

?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class logcheck extends HttpServlet {
 
 public logcheck() {
  super();
 }
 
 
 public void destroy() {
  super.destroy();
 }
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  doPost(request, response);
 }
 
 
 /*實(shí)現(xiàn)的核心代碼*/
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  response.setContentType("image/jpeg");
  HttpSession session=request.getSession();
  int width=60;
  int height=20;
  
  //設(shè)置瀏覽器不要緩存此圖片
  response.setHeader("Pragma", "No-cache");
  response.setHeader("Cache-Control", "no-cache");
  response.setDateHeader("Expires", 0);
  
  //創(chuàng)建內(nèi)存圖像并獲得圖形上下文
  BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  Graphics g=image.getGraphics();
  
  /*
   * 產(chǎn)生隨機(jī)驗(yàn)證碼
   * 定義驗(yàn)證碼的字符表
   */
  String chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  char[] rands=new char[4];
  for(int i=0;i<4;i++){
   int rand=(int) (Math.random() *36);
   rands[i]=chars.charAt(rand);
  }
  
  /*
   * 產(chǎn)生圖像
   * 畫背景
   */
  g.setColor(new Color(0xDCDCDC));
  g.fillRect(0, 0, width, height);
  
  /*
   * 隨機(jī)產(chǎn)生120個(gè)干擾點(diǎn)
   */
  
  for(int i=0;i<120;i++){
   int x=(int)(Math.random()*width);
   int y=(int)(Math.random()*height);
   int red=(int)(Math.random()*255);
   int green=(int)(Math.random()*255);
   int blue=(int)(Math.random()*255);
   g.setColor(new Color(red,green,blue));
   g.drawOval(x, y, 1, 0);
  }
  g.setColor(Color.BLACK);
  g.setFont(new Font(null, Font.ITALIC|Font.BOLD,18));
  
  //在不同高度輸出驗(yàn)證碼的不同字符
  g.drawString(""+rands[0], 1, 17);
  g.drawString(""+rands[1], 16, 15);
  g.drawString(""+rands[2], 31, 18);
  g.drawString(""+rands[3], 46, 16);
  g.dispose();
  
  //將圖像傳到客戶端
  ServletOutputStream sos=response.getOutputStream();
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  ImageIO.write(image, "JPEG", baos);
  byte[] buffer=baos.toByteArray();
  response.setContentLength(buffer.length);
  sos.write(buffer);
  baos.close();
  sos.close();
  
  session.setAttribute("checkcode", new String(rands));
 }
 
 
 public void init() throws ServletException {
  // Put your code here
 }
 
}

2.用于顯示驗(yàn)證碼的頁面

 

?
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
30
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  
  <title>index</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0"
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
 </head>
 
 <body>
 <form action="yanzheng" method="post">
   <input type="text" name="name" size="5" maxlength="4">
  <a href="index.jsp"><img border="0" src="logcheck"></a><br><br>
       <input type="submit" value="提交">
 </form>
 </body>
</html>

3.用于檢驗(yàn)輸入的驗(yàn)證碼是否正確:

?
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class yanzheng extends HttpServlet {
 
 public yanzheng() {
  super();
 }
 
 public void destroy() {
  super.destroy();
 }
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  doPost(request, response);
 }
 /*核心代碼*/
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  String info=null;
  /*獲取輸入的值*/
  String value1=request.getParameter("name");
 
  /*獲取圖片的值*/
  HttpSession session=request.getSession();
  String value2=(String)session.getAttribute("checkcode");
 
  /*對(duì)比兩個(gè)值(字母不區(qū)分大小寫)*/
  if(value2.equalsIgnoreCase(value1)){
   info="驗(yàn)證碼輸入正確";
  }else{
   info="驗(yàn)證碼輸入錯(cuò)誤";
  }
  System.out.println(info);
  request.setAttribute("info", info);
  request.getRequestDispatcher("/login.jsp").forward(request, response);
 }
 
 
 public void init() throws ServletException {
  // Put your code here
 }
 
}

4.顯示輸入結(jié)構(gòu)界面(輸入驗(yàn)證碼是否正確): 

?
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
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>">
 
 <title>My JSP 'login.jsp' starting page</title>
 
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
 
 </head>
 
 <body>
 <%=request.getAttribute("info") %>
 </body>
</html>

5.項(xiàng)目結(jié)構(gòu)、效果截圖:

java web驗(yàn)證碼實(shí)現(xiàn)代碼分享

java web驗(yàn)證碼實(shí)現(xiàn)代碼分享

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲第一色视频 | 青青草在视线频久久 | 波多野结衣被绝伦强在线观看 | 亚洲国产在线视频中文字 | 亚洲精品国产一区二区第一页 | 日本亚欧乱色视频在线观看 | 日韩高清一区二区三区不卡 | 911亚洲精品国内自产 | 成人久久18免费网站 | 国产首页精品 | 日本高清免费中文字幕不卡 | 美女扒开腿让男人桶爽动态图片 | 特黄特色大片免费影院 | 国产成人小视频在线观看 | 亚洲国产美女精品久久久久 | 精品福利一区二区免费视频 | 国产露脸对白刺激3p在线 | 国产自在线拍 | 男男浴室吸乳play | 好舒服好爽再快点视频 | 国产亚洲精品美女2020久久 | 天天做日日爱 | 亚洲国产欧美久久香综合 | 国产福利一区二区精品视频 | 韩国甜性涩爱在线播放 | 欧美一区二区三区免费看 | 97se狠狠狠狠狼亚洲综合网 | 欧美四虎影院 | 射逼网站 | 免费成年人在线视频 | 高h生子双性美人受 | 日韩一级片在线播放 | 亚洲国产精品无码中文字满 | 欧美一区二区三区四区五区六区 | 韩剧hd | 91视频无限看 | 亚洲黄色网页 | 无人视频在线观看完整版高清 | les女同h高h喷水 | 国产精品99爱免费视频 | 国产成人一区二区三区小说 |