利用 servlet 实现验证码主要继承 httpservlet 类
package com.zyc.demo; import java.awt.color; Importer java.awt.font; import java.awt.graphics; Importer java.awt.image.bufferedImage; Importer java.io.ioException; import java.util.random; import javax.imageio.imageio; Importer javax.servlet.servletException; import javax.servlet.http.httpservlet; Importer javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse; Importer javax.servlet.http.httpSession; La classe publique DrewImage étend HttpServlet {/ ** * * / private statique final long SerialVersionUID = 1505032428319459075l; Fonde finale privée mfont = nouvelle police ("Arial Black", Font.Plain, 16); Final privé int img_width = 100; Final privé int img_heigth = 18; Couleur privée getrandColor (int fc, int bc) {aléatoire aléatoire = 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); retourner une nouvelle couleur (r, g, b); } public void Service (demande httpservletRequest, réponse httpservletResponse) lève ServletException, ioException {réponse.sethEader ("pragma", "non-cache"); Response.sethEader ("Cache-Control", "No-cache"); réponse.setDateHeader ("expire", 0); réponse.setContentType ("Image / JPEG"); BufferedImage Image = new BufferedImage (img_width, img_heigth, bufferedImage.type_int_rgb); Graphiques g = image.getGraphics (); Aléatoire aléatoire = nouveau aléatoire (); g.setColor (getrandcolor (200, 250)); G.FillRect (1, 1, IMG_Width - 1, img_heigth - 1); g.setColor (nouvelle couleur (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 = ""; pour (int i = 0; i <4; i ++) {String tmp = getrandomChar (); srand + = tmp; g.setColor (nouvelle couleur (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 (); Imageo.write (image, "jpeg", réponse.getOutputStream ()); } chaîne privée getrandomchar () {int rand = (int) math.round (math.random () * 2); long itmp = 0; char ctmp = '/ u0000'; switch (rand) {cas 1: itmp = math.round (math.random () * 25 + 65); ctmp = (char) itmp; return String.ValueOf (CTMP); Cas 2: itmp = math.round (math.random () * 25 + 97); ctmp = (char) itmp; return String.ValueOf (CTMP); par défaut: 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"> <displays> industrieddemo </siffraction-name> <KELPELLE-FILE> index.html </venke-file> <leen welcome-file> index.htm </venke-file> <ven welcome-file> index.jsp </ welcome-file> <heloy-file> default.html </ bienvenue-file> </ven welcome-file> </ven welcome-file> <servlet> <Vertlet-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> <//patter-app>
jsp 文件
<% @ page langage = "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> <adread> <base href = "<% = baspath%>"> <title> mon jsp 'yanzhengma.jsp' Page de départ </ title> <meta http-equiv = "Pragma" Contenu = "> <Meta http-equiv =" Pragma "Contenu ="> <Meta http-equiv = "Pragma" Contenu = "NO-CACHE" http-equiv = "cache-control" contenu = "no-cache"> <meta http-equiv = "expires" contenu = "0"> <meta http-equiv = "keywords" contenu = "keyword1, keyword2, keyword3"> <meta http-equiv = "description" contenu = "c'est ma page"> <! type = "text / css" href = "Styles.css"> -> </ head> <body> <img src = "img.do"> <bouton onclick = "window.location.reload ();"> 刷新 </futton> </ body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。