java實現(xiàn)網(wǎng)頁驗證碼功能
本文實例為大家分享了java網(wǎng)頁驗證碼的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
Servlet:
package cn.bdqn.servlet; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.font.ImageGraphicAttribute; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; @WebServlet(name = "Servlet",urlPatterns = "/yanCode") public class Servlet extends HttpServlet { public void doPost(javax.servlet.http.HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request,response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String s="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); int width=500; int height=50; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics graphics = image.getGraphics();//相當(dāng)于畫筆 //畫背景 graphics.setColor(Color.gray); graphics.fillRect(0,0,width,height); graphics.setColor(Color.black); for (int i = 0; i <4 ; i++) { int index = random.nextInt(s.length()); String c = s.substring(index, index + 1); graphics.drawString(c,width/5*(i+1),15); } ImageIO.write(image,"jpg",response.getOutputStream()); } }
jsp:
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/10/10 Time: 13:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>驗證碼</title> </head> <body> <img src="yanCode" alt="驗證碼" style="width: 500px;height:50px"> <input type="button" value="看不清,更換驗證碼" id="btn" onclick="changeCode();"> <script type="text/javascript" src="js/jquery.min.js"> </script> <script type="text/javascript" > function changeCode() { $("img").attr('src', 'yanCode?ts=' + new Date().getTime()); } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳細(xì)介紹idea如何設(shè)置類頭注釋和方法注釋(圖文)
本篇文章主要介紹了idea如何設(shè)置類頭注釋和方法注釋(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Spring注解@Qualifier的使用&&與@Primary注解的不同
今天帶你了解一下Spring框架中的@Qualifier?注解,它解決了哪些問題,以及如何使用它,我們還將了解它與?@Primary?注解的不同之處,感興趣的朋友跟隨小編一起看看吧2023-10-10關(guān)于mybatis-plus-generator的簡單使用示例詳解
在springboot項目中集成mybatis-plus是很方便開發(fā)的,最近看了一下plus的文檔,簡單用一下它的代碼生成器,接下來通過實例代碼講解關(guān)于mybatis-plus-generator的簡單使用,感興趣的朋友跟隨小編一起看看吧2024-03-03Mybatis collection查詢集合屬性報錯的解決方案
這篇文章主要介紹了Mybatis collection查詢集合屬性報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09如何為?Spring?Boot?項目配置?Logback?日志
由于?Spring?Boot?的默認(rèn)日志框架選用的?Logback,再加上?Log4j2?之前爆過嚴(yán)重的漏洞,所以我們這次就只關(guān)注?Logback,本文重點給大家介紹如何為?Spring?Boot?項目配置?Logback?日志,感興趣的朋友跟隨小編一起看看吧2024-07-07一文了解Java Log框架徹底搞懂Log4J,Log4J2,LogBack,SLF4J
本文主要介紹了一文了解Java Log框架徹底搞懂Log4J,Log4J2,LogBack,SLF4J,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Spring Security實現(xiàn)驗證碼登錄功能
這篇文章主要介紹了Spring Security實現(xiàn)驗證碼登錄功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01