利用 servlet 实现验证码主要继承 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 { / ** * * / ส่วนตัวคงที่สุดท้าย Long SerialVersionUID = 1505032428319459075L; ตัวอักษรสุดท้ายส่วนตัว MFONT = FONT ใหม่ ("Arial Black", Font.Plain, 16); IMG_WIDTH สุดท้ายส่วนตัว = 100; IMG_HEIGTH ครั้งสุดท้ายส่วนตัว = 18; สีส่วนตัว getRandColor (int fc, int bc) {สุ่มสุ่ม = new random (); ถ้า (FC> 255) FC = 255; ถ้า (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); } บริการโมฆะสาธารณะ (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setheader ("pragma", "no-cache"); Response.Setheader ("แคชควบคุม", "ไม่มีแคช"); Response.setDateHeader ("หมดอายุ", 0); Response.SetContentType ("Image/JPEG"); BufferedImage Image = New BufferedImage (IMG_WIDTH, IMG_HEIGTH, BufferedImage.type_int_rgb); กราฟิก g = image.getGraphics (); สุ่มสุ่ม = ใหม่สุ่ม (); 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)); สำหรับ (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)); สำหรับ (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); สตริง srand = ""; สำหรับ (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 (จริง); Session.setAttribute ("Rand", Srand); // system.out.println ("写入เซสชัน"+SRAND); G.Dispose (); Imageio.write (รูปภาพ, "jpeg", response.getOutputStream ()); } สตริงส่วนตัว getRandomChar () {int rand = (int) math.round (math.random () * 2); itmp ยาว = 0; ถ่าน ctmp = '/u0000'; สวิตช์ (แรนด์) {กรณีที่ 1: itmp = math.round (math.random () * 25 + 65); CTMP = (ถ่าน) ITMP; return string.valueof (CTMP); กรณีที่ 2: itmp = math.round (math.random () * 25 + 97); CTMP = (ถ่าน) ITMP; return string.valueof (CTMP); ค่าเริ่มต้น: itmp = math.round (math.random () * 9); ส่งคืน ITMP + ""; -下面是 web.xml 配置
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <web-app xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xmlns = "http://java.sun.com/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" เวอร์ชัน = "3.0"> <welcome-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html <welcome-file> <servlet-name> img </servlet-name> <servlet-class> com.zyc.demo.drewimage </servlet-lass> </servlet> <servlet-mapping> <servlet-name> img </servlet-name>
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> jsp 'yanzhengma.jsp' http-equiv = "cache-control" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> <meta http-equiv = "คำหลัก" content = "keyword1, คำหลัก 2, คำหลัก type = "text/css" href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onclick = "window.location.reload ();"> 刷新 </button> </body> </html>
简单实用。
以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。