利用 Сервлет 实现验证码主要继承 httpservlet 类
пакет com.zyc.demo; импортировать java.awt.color; импортировать java.awt.font; импортировать java.awt.graphics; Импорт java.awt.image.bufferedImage; импортировать java.io.ioexception; импортировать java.util.random; Импорт javax.imageio.imageio; Импорт javax.servlet.servletexception; Импорт javax.servlet.http.httpservlet; Импорт javax.servlet.http.httpservletrequest; Импорт javax.servlet.http.httpservletresponse; Импорт javax.servlet.http.httpsession; открытый класс Drewimage расширяет httpservlet { / ** * * / private static final long serialversionuid = 1505032428319459075l; Частный финальный шрифт mfont = new Font ("Arial Black", font.plain, 16); Приватный окончательный int img_width = 100; Приватный финал int img_heigth = 18; Частный цвет getrandcolor (int fc, int bc) {случайный случайный = 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); вернуть новый цвет (r, g, b); } public void service (httpservletrequest, httpservletresponse response), бросает ServletException, ioException {response.setheader ("pragma", "без Cache"); response.setheader («Контроль кэша», «без каша»); response.setDateHeader («истекает», 0); response.setContentType ("Image/jpeg"); BufferedImage Image = new BufferedImage (img_width, img_heigth, bufferedimage.type_int_rgb); Graphics g = image.getGraphics (); Случайный случайный = new Random (); g.setcolor (getrandcolor (200, 250)); G.FillRect (1, 1, img_width - 1, img_heigth - 1); G.SetColor (новый цвет (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 (новый цвет (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 ()); } частная строка getrandomChar () {int rand = (int) math.round (math.random () * 2); длинный itmp = 0; char ctmp = '/u0000'; Switch (rand) {case 1: itmp = math.round (math.random () * 25 + 65); ctmp = (char) itmp; return string.valueof (ctmp); Случай 2: itmp = math.round (math.random () * 25 + 97); ctmp = (char) itmp; return string.valueof (ctmp); по умолчанию: itmp = math.round (math.random () * 9); вернуть 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"> <Slace-Name> IndustradDemo </webApp_ID "3,0"> <SIPLE-NAME> </wyply-fist-name </webApp_id ". <welcelcome-file> index.html </wervelodefile> <selcoveryfile> index.htm </wervedied file> <selcownfile> index.jsp </wervelode-file> <welcelporting> default.html </werveload file> <bellage-file> default.htm </werveload> <welcome-file> deffile. <servlet-name> img </servlet-name> <vervlet-class> com.zyc.demo.drewimage </servlet> </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' nate-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte-conte- http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "expires" content = "0"> <meta http-equiv = "Content" Content = ", ключевое слово1, ключевое слово3"> <meta http-equiv = "description". type = "text/css" href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onclick = "window.location.reload ();"> 刷新 </button> </body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 也希望大家多多支持武林网。