利用 Servlet 实现验证码主要继承 httpServlet 类
Paket com.zyc.demo; Import Java.awt.Color; import Java.awt.font; importieren java.awt.graphics; Import Java.awt.image.BuffenedImage; importieren java.io.ioException; import Java.util.random; import Javax.imageo.imageo; importieren javax.servlet.servletException; import Javax.servlet.http.httpServlet; importieren javax.servlet.http.httpServletRequest; importieren javax.servlet.http.httpServletResponse; import Javax.servlet.http.httpSession; öffentliche Klasse Drewimage erweitert HttpServlet { / ** * * / Private statische endgültige lange Serialversionuid = 1505032428319459075L; Private Final Font Mfont = New Font ("Arial Black", Font.Plain, 16); private endgültige int img_width = 100; private endgültige int img_hieth = 18; Private Color 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); Neue Farbe zurückgeben (r, g, b); } public void Service (httpServletRequest-Anfrage, httpServletResponse-Antwort) löst ServletException aus, ioException {response.setheader ("pragma", "no-cache"); response.setheader ("cache-control", "no-cache"); response.setDateHeader ("läuft", 0); Antwort.SetContentType ("Image/JPEG"); Bufferedimage image = new bufferedImage (img_width, img_hieth, bufferedimage.type_int_rgb); Grafik g = image.getGraphics (); Random random = new random (); G.SetColor (getrandcolor (200, 250)); G. -fillRect (1, 1, img_width - 1, img_hieth - 1); G.SetColor (neue Farbe (102, 102, 102)); g.drawrect (0, 0, img_width - 1, img_hieth - 1); G.SetColor (getrandcolor (160.200)); für (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)); für (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 = ""; für (int i = 0; i <4; i ++) {String tmp = getRandomchar (); srand += tmp; G.SetColor (neue Farbe (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.disponse (); Imageio.Write (Bild, "JPEG", response.getOutputStream ()); } private String getRandomchar () {int rand = (int) math.round (math.random () * 2); lange ITMP = 0; char ctmp = '/u0000'; Switch (rand) {case 1: itmp = math.round (math.random () * 25 + 65); ctmp = (char) itmp; return String.Valueof (CTMP); Fall 2: itmp = math.round (math.random () * 25 + 97); ctmp = (char) itmp; return String.Valueof (CTMP); Standard: itmp = math.round (math.random () * 9); kehren Sie ITMP + "" zurück; }}}下面是 web.xml 配置
<? 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 "3.0"-"3.0" --NAME> </display-name> <Welcome-File> Index.html </Welcome-File> <Welcome-File> Index.htm </Welcome-File> <Welcome-File> Index.jsp </Welcome-File> <Welcome-File> Standard. <servlet-name> img </servlet-name> <Servlet-Klasse> com.zyc.demo.drewimage </Servlet-Klasse> </Servlet> <Servlet-Mapping> <Servlet-name> img </Servlet-name> <url-pattern> /img.do </url-patter> </servlet-maping> </web-app>
JSP 文件
<%@ page Language = "java" import = "java.util. 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 'yanzheng.jsp' Startpage </title> <meta http-equiv = "ada- meta http-equive http-äquiv = "cache-control" content = "no-cache"> <meta http-äquiv = "expires" content = "0"> <meta http-äquiv type = "text/css" href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onclick = "window.location.reload ();"> 刷新 </button> </body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。