欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java使用servlet實(shí)現(xiàn)驗(yàn)證碼

 更新時(shí)間:2017年01月05日 11:41:13   作者:zhaoyachao123  
這篇文章主要介紹了java使用servlet實(shí)現(xiàn)驗(yàn)證碼,簡(jiǎn)單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

利用servlet 實(shí)現(xiàn)驗(yàn)證碼主要繼承httpServlet類

package com.zyc.demo; 
 
import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.util.Random; 
 
import javax.imageio.ImageIO; 
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 DrewImage extends HttpServlet{ 
  /** 
   * 
   */ 
  private static final long serialVersionUID = 1505032428319459075L; 
  private final Font mFont = 
      new Font("Arial Black", Font.PLAIN, 16); 
    private final int IMG_WIDTH = 100; 
    private final int IMG_HEIGTH = 18; 
    private Color getRandColor(int fc,int bc) 
    { 
      Random random = new Random(); 
      if(fc > 255) fc = 255; 
      if(bc > 255) bc=255; 
      int r = fc + random.nextInt(bc - fc); 
      int g = fc + random.nextInt(bc - fc); 
      int b = fc + random.nextInt(bc - fc); 
      return new Color(r , g , b); 
    } 
    public void service(HttpServletRequest request, 
      HttpServletResponse response) 
      throws ServletException, IOException 
    { 
      response.setHeader("Pragma","No-cache"); 
      response.setHeader("Cache-Control","no-cache"); 
      response.setDateHeader("Expires", 0); 
      response.setContentType("image/jpeg"); 
      BufferedImage image = new BufferedImage 
        (IMG_WIDTH , IMG_HEIGTH , BufferedImage.TYPE_INT_RGB); 
      Graphics g = image.getGraphics(); 
      Random random = new Random(); 
      g.setColor(getRandColor(200 , 250)); 
      g.fillRect(1, 1, IMG_WIDTH - 1, IMG_HEIGTH - 1); 
      g.setColor(new Color(102 , 102 , 102)); 
      g.drawRect(0, 0, IMG_WIDTH - 1, IMG_HEIGTH - 1); 
      g.setColor(getRandColor(160,200)); 
      for (int i = 0 ; i < 30 ; i++) 
      { 
        int x = random.nextInt(IMG_WIDTH - 1); 
        int y = random.nextInt(IMG_HEIGTH - 1); 
        int xl = random.nextInt(6) + 1; 
        int yl = random.nextInt(12) + 1; 
        g.drawLine(x , y , x + xl , y + yl); 
      } 
      g.setColor(getRandColor(160,200)); 
      for (int i = 0 ; i < 30 ; i++) 
      { 
        int x = random.nextInt(IMG_WIDTH - 1); 
        int y = random.nextInt(IMG_HEIGTH - 1); 
        int xl = random.nextInt(12) + 1; 
        int yl = random.nextInt(6) + 1; 
        g.drawLine(x , y , x - xl , y - yl); 
      } 
      g.setFont(mFont); 
      String sRand = ""; 
      for (int i = 0 ; i < 4 ; i++) 
      { 
        String tmp = getRandomChar(); 
        sRand += tmp; 
        g.setColor(new Color(20 + random.nextInt(110) 
          ,20 + random.nextInt(110) 
          ,20 + random.nextInt(110))); 
        g.drawString(tmp , 15 * i + 10,15); 
      } 
      HttpSession session = request.getSession(true); 
      session.setAttribute("rand" , sRand); 
//     System.out.println("寫入session"+sRand); 
      g.dispose(); 
      ImageIO.write(image, "JPEG", response.getOutputStream()); 
    } 
    private String getRandomChar() 
    { 
      int rand = (int)Math.round(Math.random() * 2); 
      long itmp = 0; 
      char ctmp = '\u0000'; 
      switch (rand) 
      { 
        case 1: 
          itmp = Math.round(Math.random() * 25 + 65); 
          ctmp = (char)itmp; 
          return String.valueOf(ctmp); 
        case 2: 
          itmp = Math.round(Math.random() * 25 + 97); 
          ctmp = (char)itmp; 
          return String.valueOf(ctmp); 
        default : 
          itmp = Math.round(Math.random() * 9); 
          return itmp + ""; 
      } 
    } 
} 

下面是web.xml 配置

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
 <display-name>IndustryDemo</display-name> 
 <welcome-file-list> 
  <welcome-file>index.html</welcome-file> 
  <welcome-file>index.htm</welcome-file> 
  <welcome-file>index.jsp</welcome-file> 
  <welcome-file>default.html</welcome-file> 
  <welcome-file>default.htm</welcome-file> 
  <welcome-file>default.jsp</welcome-file> 
 </welcome-file-list> 
  
 <servlet> 
   <servlet-name>img</servlet-name> 
   <servlet-class>com.zyc.demo.DrewImage</servlet-class> 
 </servlet> 
  
 <servlet-mapping> 
   <servlet-name>img</servlet-name> 
   <url-pattern>/img.do</url-pattern> 
 </servlet-mapping> 
  
</web-app> 

 jsp 文件

<%@ 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 'yanzhengma.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> 
  <img alt="驗(yàn)證碼" src="img.do"> <button onclick="window.location.reload();">刷新</button> 
 </body> 
</html> 

簡(jiǎn)單實(shí)用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java使用for循環(huán)輸出楊輝三角

    java使用for循環(huán)輸出楊輝三角

    楊輝三角形由數(shù)字排列,可以把它看做一個(gè)數(shù)字表,其基本特性是兩側(cè)數(shù)值均為1,其他位置的數(shù)值是其正上方的數(shù)字與左上角數(shù)值之和,下面是java使用for循環(huán)輸出包括10行在內(nèi)的楊輝三角形
    2014-02-02
  • Spring中的AutowireCandidateResolver的具體使用詳解

    Spring中的AutowireCandidateResolver的具體使用詳解

    這篇文章主要介紹了Spring中的AutowireCandidateResolver的具體使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Centos7安裝JDK1.8詳細(xì)過(guò)程實(shí)戰(zhàn)記錄

    Centos7安裝JDK1.8詳細(xì)過(guò)程實(shí)戰(zhàn)記錄

    這篇文章主要給大家介紹了關(guān)于Centos7安裝JDK1.8的相關(guān)資料,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    詳解Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性

    Chrome 51 開(kāi)始,瀏覽器的 Cookie 新增加了一個(gè)SameSite屬性,用來(lái)防止 CSRF 攻擊和用戶追蹤。今天通過(guò)本文給大家介紹Springboot應(yīng)用中設(shè)置Cookie的SameSite屬性,感興趣的朋友一起看看吧
    2022-01-01
  • java中Spring Security的實(shí)例詳解

    java中Spring Security的實(shí)例詳解

    這篇文章主要介紹了java中Spring Security的實(shí)例詳解的相關(guān)資料,spring security是一個(gè)多方面的安全認(rèn)證框架,提供了基于JavaEE規(guī)范的完整的安全認(rèn)證解決方案,需要的朋友可以參考下
    2017-09-09
  • IDEA 2020代碼提示忽略大小寫的問(wèn)題

    IDEA 2020代碼提示忽略大小寫的問(wèn)題

    這篇文章主要介紹了IDEA 2020代碼提示忽略大小寫的問(wèn)題,本文通過(guò)圖文并茂的形式給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式

    SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式

    這篇文章主要給大家介紹了關(guān)于SpringBoot啟動(dòng)時(shí)自動(dòng)執(zhí)行代碼的幾種實(shí)現(xiàn)方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Springboot 接口對(duì)接文件及對(duì)象的操作方法

    Springboot 接口對(duì)接文件及對(duì)象的操作方法

    這篇文章主要介紹了Springboot 接口對(duì)接文件及對(duì)象的操作,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • springboot使用線程池(ThreadPoolTaskExecutor)示例

    springboot使用線程池(ThreadPoolTaskExecutor)示例

    大家好,本篇文章主要講的是springboot使用線程池(ThreadPoolTaskExecutor)示例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • SpringMVC中使用bean來(lái)接收f(shuō)orm表單提交的參數(shù)時(shí)的注意點(diǎn)

    SpringMVC中使用bean來(lái)接收f(shuō)orm表單提交的參數(shù)時(shí)的注意點(diǎn)

    本篇文章主要介紹了SpringMVC中使用bean來(lái)接收f(shuō)orm表單提交的參數(shù)時(shí)的注意點(diǎn),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-05-05

最新評(píng)論