利用 Servlet 实现验证码主要继承 httpservlet 类
paquete 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; La clase pública drewimage extiende httpservlet { / ** * * / private static final long serialversionUid = 1505032428319459075l; FUNT FINAL PRIVADO MFONT = nueva fuente ("Arial Black", Font.Plain, 16); Private final int img_width = 100; Private final int img_heigth = 18; Color privado 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); devolver nuevo color (R, G, B); } Servicio público void (solicitud httpservletRequest, httpservletResponse respuesta) arroja servletException, ioexception {respuesta.setheader ("pragma", "no-cache"); Respuesta.setheader ("Cache-Control", "No-Cache"); respuesta.setDateHeader ("expiras", 0); respuesta.setContentType ("Image/jpeg"); BufferedImage Image = new BufferedImage (img_width, img_heigth, bufferedImage.type_int_rgb); Gráficos g = image.getgraphics (); Aleatorio aleatorio = new Random (); G.SetColor (GetRandcolor (200, 250)); g.fillrect (1, 1, img_width - 1, img_heigth - 1); G.SetColor (nuevo 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); Cadena srand = ""; for (int i = 0; i <4; i ++) {string tmp = getRandomchar (); srand += tmp; G.SetColor (nuevo 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 ()); } String private getRandomchar () {int rand = (int) Math.round (math.random () * 2); largo itmp = 0; char ctmp = '/u0000'; switch (rand) {caso 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); predeterminado: itmp = math.round (math.random () * 9); devolver itmp + ""; }}}下面是 web.xml 配置
<? xml versión = "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"> <sipsipname> Industrydemo </visual <Sciente-File> Index.html </aude-file> <Welcome-file> index.htm </velcente--file> <calentable--file> index.jsp </leadde-file> <welcome-file> default.html </bevie--file> <calle-file> default.htm </welcome-file> <beating-file> default.jsp </breve-file-file> <ervlet> <ervlet> <Servlet-name> img </servlet-name> <ervlet-class> com.zyc.demo.drewimage </servlet-class> </servlet> <servlet-mapping> <ervlet-name> img </servlet-name> <url-pattern> /img.do </url-pattern> </servlet-mapping> </beb-app>
JSP 文件
<%@ página lenguaje = "java" import = "java.util.*" pageEncoding = "utf-8"%> <%string path = requit.getContextPath (); String basepath = request.getScheme ()+": //"+request.getServerName ()+":"+request.getServerPort ()+ruta+"/"; %> < http-equiv = "cache-confontrol" content = "no-cache"> <meta http-equiv = "expires" content = "0"> <meta http-equiv = "palabras clave" content = "keyword1, weyword 2, keyword3"> <meta http-oquiv = "descripción" Content = "esto es mi página" << type = "text/css" href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onClick = "window.location.reload ();"> 刷新 </botón> </body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 也希望大家多多支持武林网。 也希望大家多多支持武林网。