亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實現(xiàn)驗證碼小程序

 更新時間:2018年04月26日 15:13:28   作者:糖拌西紅柿  
這篇文章主要為大家詳細介紹了java實現(xiàn)驗證碼小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下

手動制作java的驗證碼,具體內(nèi)容如下

Web應用驗證碼的組成:

(1)輸入框

(2)顯示驗證碼的圖片

驗證碼的制作流程:

生成驗證碼的容器使用 j2ee的servlet

生成圖片需要的類:

(1) BufferedImage 圖像數(shù)據(jù)緩沖區(qū)

(2)Graphic繪制圖片

(3)Color獲取顏色

(4)Random生成隨機數(shù)

(5)ImageIO輸入圖片

具體實現(xiàn)流程:

(1)定義一個Servlet,用于生成驗證碼

(2)定已BufferedImage對象,主要作用就是制作一個圖片緩沖區(qū),作為圖片的一個臨時容器。

(3)獲得Graphic對象,畫圖的“背景”,理解為"畫布"

(4)通過Random生成隨機數(shù),來制作驗證信息

(5)通過Graphic操作,進行具體畫圖

(6)信息存至session中

(7)使用ImageIO輸出生成的圖片,通過設置ImageIO的 write()方法中的out參數(shù),response.getOutputStream(),將圖片傳至前臺

(8)制作驗證用的servlet,提取session中的數(shù)據(jù)進行驗證(這里采用ajax異步方法)

具體代碼實現(xiàn)(Web端驗證碼實例)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>java驗證碼</title>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
 驗證碼輸入:<input type="text" name="checkcode"> &nbsp;
 <img id="codeimg" alt="驗證碼" src="ImageServlet">
 <a href="javascript:reloadCode();">看不出清楚?</a><br/>
 <input type="submit" value="提交">
 <div id="info" style="color:red;"></div>
 
 <script type="text/javascript">
 $(function(){
  //ajax異步傳驗證碼至后臺
  $("input[type=submit]").click(function(){
   $.post("CheckCode",
     {"code":$("input[name=checkcode]").val()},
     function(data,textStatus)
     {
      console.log(textStatus);
      $("#info").html(data);
     },"text");
  });
 })
 //js刷新,重新請求頁面,獲得新的驗證碼
 function reloadCode(){
  var time = new Date().getTime();//創(chuàng)建不同的時間
  $("#codeimg").attr("src","ImageServlet?time="+time);//因為時間參數(shù)不同,請求重新
 }
 </script> 
</body>
</html>

ImageServlet類(驗證碼生成部分)

@WebServlet("/ImageServlet")
public class ImageServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
  
 /**
  * @see HttpServlet#HttpServlet()
  */
 public ImageServlet() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  BufferedImage bimg = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
  Graphics g = bimg.getGraphics();
  Color color = new Color(200,151,255);//顏色生成
  g.setColor(color);
  g.fillRect(0, 0, 68, 22);
  char[] ch="ABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789".toCharArray();//獲得一個含有字母和數(shù)字的數(shù)組,后續(xù)隨機從中獲取字符
  Random random = new Random(); //創(chuàng)建隨機數(shù)
  int len = ch.length,index;
  StringBuffer sBuffer = new StringBuffer();
  
  //循環(huán)產(chǎn)生4個隨機字符
  for(int i = 0 ;i<4;i++)
  {
   index = random.nextInt(len);//產(chǎn)生隨機字母與數(shù)字
   g.setColor(new Color(random.nextInt(88),random.nextInt(120),random.nextInt(90)));//創(chuàng)建隨機顏色
   g.drawString(ch[index]+"", (i)*15+3, 18);
   sBuffer.append(ch[index]);//將隨機獲取的字符放置緩沖串中
  }
  request.getSession().setAttribute("newCode", sBuffer.toString());//存至session便于后續(xù)驗證
  ImageIO.write(bimg, "JPG", response.getOutputStream());//通過ImageIO輸出圖片,并傳至前臺
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

CheckCode(驗證碼驗證servlet) 

@WebServlet("/CheckCode")
public class CheckCode extends HttpServlet {
 private static final long serialVersionUID = 1L;
  
 /**
  * @see HttpServlet#HttpServlet()
  */
 public CheckCode() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setCharacterEncoding("utf-8");
  String code = request.getParameter("code").toUpperCase();//獲取傳來驗證碼并進行大小寫轉換
  String result=null;
  if(request.getSession().getAttribute("newCode").equals(code))
  {
   result="驗證成功!";
   response.getWriter().append(result);
  }
  else {
   result="驗證碼錯誤!";
   response.getWriter().append(result);
  }
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Mybatisplus自動填充實現(xiàn)方式及代碼示例

    Mybatisplus自動填充實現(xiàn)方式及代碼示例

    這篇文章主要介紹了Mybatisplus自動填充實現(xiàn)方式及代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • java中重載、覆蓋和隱藏三者的區(qū)別分析

    java中重載、覆蓋和隱藏三者的區(qū)別分析

    本篇文章介紹了,在java中重載、覆蓋和隱藏它們?nèi)叩膮^(qū)別分析。需要的朋友參考下
    2013-04-04
  • 詳解SpringBoot的三種緩存技術(Spring Cache、Layering Cache 框架、Alibaba JetCache 框架)

    詳解SpringBoot的三種緩存技術(Spring Cache、Layering Cache 框架、Alibaba J

    這篇文章主要介紹了SpringBoot的三種緩存技術,幫助大家更好的理解和學習springboot框架,感興趣的朋友可以了解下
    2020-10-10
  • Spring Boot讀取配置屬性常用方法解析

    Spring Boot讀取配置屬性常用方法解析

    這篇文章主要介紹了Spring Boot讀取配置屬性常用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • IDEA插件之mybatisx?插件使用教程

    IDEA插件之mybatisx?插件使用教程

    這篇文章主要介紹了mybatisx?插件使用教程,包括插件安裝自動生成代碼的相關知識,本文通過實例圖文相結合給大家介紹的非常詳細,需要的朋友可以參考下
    2022-05-05
  • IDEA中GitLab的使用詳解

    IDEA中GitLab的使用詳解

    這篇文章主要介紹了IDEA中GitLab的使用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-07-07
  • Swagger的使用教程詳解

    Swagger的使用教程詳解

    Swagger是一個強大的API文檔工具,它能夠簡化API文檔的編寫和維護工作,提供了一種方便的方式來描述、展示和測試RESTful風格的Web服務接口,本文介紹了Swagger的安裝配置和使用方法,并提供了示例代碼,感興趣的朋友一起學習吧
    2023-06-06
  • SpringBoot定時監(jiān)聽RocketMQ的NameServer問題及解決方案

    SpringBoot定時監(jiān)聽RocketMQ的NameServer問題及解決方案

    這篇文章主要介紹了SpringBoot定時監(jiān)聽RocketMQ的NameServer問題及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • Spring Cloud 配置中心內(nèi)容加密的配置方法

    Spring Cloud 配置中心內(nèi)容加密的配置方法

    這篇文章主要介紹了Spring Cloud 配置中心內(nèi)容加密的配置方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • 圖文詳解Java中的字節(jié)輸入與輸出流

    圖文詳解Java中的字節(jié)輸入與輸出流

    在Java中所有數(shù)據(jù)都是使用流讀寫的,流是一組有序的數(shù)據(jù)序列,將數(shù)據(jù)從一個地方帶到另一個地方,這篇文章主要給大家介紹了關于Java中字節(jié)輸入與輸出流的相關資料,需要的朋友可以參考下
    2021-08-08

最新評論