利用 Servlet 实现验证码主要继承 httpservlet 类
paket com.zyc.demo; impor java.awt.color; impor java.awt.font; impor java.awt.graphics; impor java.awt.image.bufferedimage; impor java.io.ioException; impor java.util.random; impor javax.imageio.imageio; impor javax.servlet.servletException; impor javax.servlet.http.httpservlet; impor javax.servlet.http.httpservletRequest; impor javax.servlet.http.httpservletResponse; impor javax.servlet.http.httpsession; Drewimage kelas publik memperluas httpservlet { / ** * * / private static final long serialversionuid = 1505032428319459075l; font font font private = font baru ("Arial Black", Font.Plain, 16); private final int img_width = 100; private final int img_heigth = 18; Private Color getRandColor (int fc, int bc) {acak acak = acak baru (); 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); mengembalikan warna baru (r, g, b); } public void service (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {response.setHeader ("pragma", "no-cache"); response.setheader ("cache-control", "no-cache"); response.setDateheader ("kedaluwarsa", 0); response.setContentType ("Image/JPEG"); BufferedImage Image = BufferedImage baru (img_width, img_heigth, bufferedimage.type_int_rgb); Grafik g = image.getGraphics (); Acak acak = acak baru (); G.SetColor (GetRandColor (200, 250)); g.fillrect (1, 1, img_width - 1, img_heigth - 1); g.setColor (warna baru (102, 102, 102)); g.drawrect (0, 0, img_width - 1, img_heigth - 1); G.SetColor (GetRandColor (160.200)); untuk (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)); untuk (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 = ""; untuk (int i = 0; i <4; i ++) {string tmp = getRandomChar (); srand += tmp; g.setColor (warna baru (20 + acak.nextint (110), 20 + random.nextint (110), 20 + acak.nextint (110))); G.DrawString (TMP, 15 * I + 10,15); } Httpsession session = request.getSession (true); session.setAttribute ("rand", srand); // System.out.println ("写入 sesi"+srand); g.dispose (); Imageo.write (gambar, "jpeg", response.getoutputStream ()); } private string getRandomChar () {int rand = (int) math.round (math.random () * 2); long itmp = 0; char ctmp = '/u0000'; switch (rand) {case 1: itmp = math.round (math.random () * 25 + 65); ctmp = (char) itmp; return string.valueof (ctmp); Kasus 2: ITMP = Math.round (Math.Random () * 25 + 97); ctmp = (char) itmp; return string.valueof (ctmp); default: itmp = math.round (math.random () * 9); mengembalikan 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/xmlns =" http://java.com/xmlns = "http://java.com/xmlns = 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"> <Display--name> </delay-noMe </delay-n-no </delay-n-no </delay-n-no </noMe-n-noMe </delay-n-noMe </delay, versi 'wevelder-n-noMETET> <creend Welcome-File> index.html </celcome-file> <creendak-file> index.htm </celcome-file> <creendak-filse> index.jsp </celcome-file> <creendak-file> default.html </celcome-file> <creend-file> default.htm </Welcome-file> <creendak-file> </Welcome-file </Welcome-file </Welcome-file> <cread-file> Default.jsp.-file> Default.htm </celcome-file> <cread-file> Default.jsp.-file> Default.htm </Welcome-File> <cread-file> Default.jspil <servlet-name> img </servlet-name> <servlet-class> com.zyc.demo.drewimage </servlet-class> </servlet> <servlet-papping> <servlet-name> img </servlet-name> <rerl-pattern> /img.do </url-pola> </servlet> </webpatper>
JSP 文件
<%@ halaman bahasa = "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 transisi // en"> <html> <head> <base href = "<%= Basepath%>"> <itement> JSP 'yanzhengma = "no-conte" metA-content = "meta-content" metA-content = "metA-conte =" metA-conte "no-conte" metA-content "metA-content = http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "kedaluwarsa" konten = "0"> <meta http-equiv = "kata kunci" konten = "kata kunci1, kata kunci2, koki"> <meta http http-equiv = "koki" ini = "ini" type = "text/css" href = "styles.css"> -> </pead> <body> <img src = "img.do"> <tombol onclick = "window.location.reload ();"> 刷新 </button> </body> </html>
简单实用。
以上就是本文的全部内容 , 希望对大家的学习有所帮助 , 也希望大家多多支持武林网。