利用 servlet 实现验证码主要继承 httpServlet 类
pacote com.zyc.demo; importar java.awt.color; importar java.awt.font; importar java.awt.graphics; importar java.awt.image.bufferiMage; importar java.io.ioException; importar java.util.random; importar javax.imageio.imageio; importar javax.servlet.servletexception; importar javax.servlet.http.httpServlet; importar javax.servlet.http.httpServletRequest; importar javax.servlet.http.httpServletResponse; importar javax.servlet.http.httpSession; classe pública Drewimage estende httpServlet { / ** * * / private estático final serialversionuid = 1505032428319459075l; Fonte final privada mfont = nova fonte ("Arial Black", font.plain, 16); private final int img_width = 100; private final int img_heigth = 18; color privado getrandcolor (int fc, int bc) {aleatório aleatório = novo aleatório (); if (fc> 255) fc = 255; if (bc> 255) bc = 255; int r = fc + aleatoriamente.nextInt (bc - fc); int g = fc + aleatoriamente.nextInt (bc - fc); int b = fc + aleatoriamente.nextInt (bc - fc); retornar nova cor (R, G, B); } Serviço public void (solicitação httpServletRequest, httpServletResponse Response) lança servletexception, ioexception {Response.setheader ("Pragma", "sem cache"); Response.setheader ("Cache-Control", "No-Cache"); Response.SetDateHeader ("Expira", 0); Response.setContentType ("Image/jpeg"); BufferImage imagem = new bufferImage (img_width, img_heigth, bufferImage.type_int_rgb); Gráficos g = image.getGraphics (); Aleatório aleatório = novo aleatório (); G.SetColor (Getrandcolor (200, 250)); G.FillRect (1, 1, img_width - 1, img_heigth - 1); g.setColor (nova cor (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 = aleatom.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 = aleatom.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 (nova cor (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); ITMP longo = 0; char ctmp = '/u0000'; switch (rand) {case 1: itmp = math.round (math.random () * 25 + 65); ctmp = (char) itmp; return string.valueof (ctmp); Caso 2: ITMP = Math.Round (Math.Random () * 25 + 97); ctmp = (char) itmp; return string.valueof (ctmp); padrão: itmp = math.round (math.random () * 9); retornar 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" = "3.0">>>>> <lorde-File> index.html </ld-Welcome> <lmed-File> index.htm </-Welcome-File> <lorde-File> Index.jsp </limk-File> <lorde-File> Default.html </-Welcome-file> <lorde-file> default.htm </welcomet-file> <lorde> </-FILE> </-FILE> default.htm </welcome-file> <lorda> <lort> <Verlet-name> img </irvlet-name> <Servlet-class> com.zyc.demo.drewimage </servlet-class> </servlet> <sirtlet-mapping> <sutlet-name> img </servlet-name> <url-stattern> /img.do </url-tattern> </ervlet-mappping>
JSP 文件
<%@ página de página = "java" import = "java.util.*" pageEncoding = "utf-8"%> <%string path = request.getContextPath (); String basalepath = request.getScheme ()+": //"+request.getServername ()+":"+request.getServerport ()+path+"/"; %> <! Doctype html public "-// w3c // dtd html 4.01 transitória // pt"> <html> <head> <base href = "<%= Basepath%>"> <title> meu jsp 'yanzhengma.jsp' inicial) http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "expire" content = "0"> <meta http-equiv = "palavras-chave" content = "keyword1, keyword2, palavra-chave"> <magle http-equiv = "descrição" "content1, keyword2, palavra-chave"> <magle http-ouquiv = "descrição" " type = "text/css" href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onclick = "window.location.reload ();"> 刷新 </button> </body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。