程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java web驗證碼完成代碼分享

java web驗證碼完成代碼分享

編輯:關於JAVA

java web驗證碼完成代碼分享。本站提示廣大學習愛好者:(java web驗證碼完成代碼分享)文章只能為提供參考,不一定能成為您想要的結果。以下是java web驗證碼完成代碼分享正文


驗證碼的感化:平日的登錄或許注冊體系時,都邑請求用戶輸出驗證碼,以此差別用戶行動和盤算機法式行動,目標是有人避免歹意注冊、暴力破解暗碼等。

完成驗證碼的思緒:用 server 完成隨機生成數字和字母構成圖片的功效,用 jsp 頁面完成顯示驗證碼和用戶輸出驗證碼的功效,再用 server 類分離獲得圖片和用戶輸出的數據,斷定兩個數據能否分歧。 
代碼完成 

1.編寫數字、英文隨機生成的 server 類,源碼:

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);
 }


 /*完成的焦點代碼*/
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("image/jpeg");
  HttpSession session=request.getSession();
  int width=60;
  int height=20;
  
  //設置閱讀器不要緩存此圖片
  response.setHeader("Pragma", "No-cache");
  response.setHeader("Cache-Control", "no-cache");
  response.setDateHeader("Expires", 0);
  
  //創立內存圖象並取得圖形高低文
  BufferedImage image=new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  Graphics g=image.getGraphics();
  
  /*
   * 發生隨機驗證碼
   * 界說驗證碼的字符表
   */
  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);
  }
  
  /*
   * 發生圖象
   * 畫配景
   */
  g.setColor(new Color(0xDCDCDC));
  g.fillRect(0, 0, width, height);
  
  /*
   * 隨機發生120個攪擾點
   */
  
  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));
  
  //在分歧高度輸入驗證碼的分歧字符
  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.用於顯示驗證碼的頁面:

<%@ 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.用於磨練輸出的驗證碼能否准確:

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");

  /*比較兩個值(字母不辨別年夜小寫)*/
  if(value2.equalsIgnoreCase(value1)){
   info="驗證碼輸出准確";
  }else{
   info="驗證碼輸出毛病";
  }
  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.顯示輸出構造界面(輸出驗證碼能否准確): 

<%@ 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.項目構造、後果截圖:

以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved