servlet 实现验证码主要继承 httpservlet 类
패키지 com.zyc.demo; import java.awt.color; Java.awt.Font 가져 오기; java.awt.graphics 가져 오기; java.awt.image.bufferedimage import; import java.io.ioexception; java.util.random import; import javax.imageio.imageio; javax.servlet.servletexception import; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; 공개 클래스 Drewimage는 httpservlet { / ** * * / private static final long serialversionuid = 1505032428319459075L; 개인 최종 글꼴 mfont = 새로운 글꼴 ( "Arial Black", font.plain, 16); 개인 최종 int img_width = 100; 개인 최종 IMG_HEIGTH = 18; 개인 색상 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); 새로운 색을 반환합니다 (R, G, B); } public void service (httpservletrequest 요청, httpservletResponse 응답) servletexception, ioexception {response.setheader ( "pragma", "no-cache"); Response.SetHeader ( "Cache-Control", "No-Cache"); response.setDateHeader ( "만료", 0); Response.setContentType ( "image/jpeg"); bufferedImage image = new bufferedImage (img_width, img_heigth, bufferedimage.type_int_rgb); 그래픽 g = image.getGraphics (); 랜덤 random = new random (); G. 세트 콜러 (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. 세트 콜러 (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); 문자열 srand = ""; for (int i = 0; i <4; i ++) {문자열 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 ( "写入 세션"+srand); g.dispose (); imageio.write (image, "jpeg", response.getOutputStream ()); } private String getRandomchar () {int rand = (int) math.round (math.random () * 2); 긴 ITMP = 0; char ctmp = '/u0000'; 스위치 (rand) {사례 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 = "weacp_id"version = "3.0 <splay-name> inditentic-name> <collect-file> index.html </welcome-file> <welcome-file> index.htm </welcome-file> <welcome-file> index.jsp </welcome-file> <welcome-file> default.html </welcome-file> <welcome-file> default.htm </welcome-file> <welcome-file> default.jsp <jsp> </file> <servlet-name> img </servlet-name> <servlet-class> com.zyc.demo.drewimage </servlet-class> </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 전환 // en"> <html> <head> <base href = "<%= basepath%>"> <title> 내 jsp 'yanzhengma.jsp'시작 페이지 </title> <meta http- equev = "pragma" "pragma" " http-equiv = "Cache-control"content = "no-cache"> <meta http-equiv = "chewors"content = "0"> <meta http-equiv = "keyword ="keyword1, keyword2, keyword2, keyword3 "> <meta http-equiv ="descript "> <" "rel rel rele"> type = "text/css"href = "styles.css"> -> </head> <body> <img src = "img.do"> <button onclick = "window.location.reload ();"> </bodult> </html>
简单实用。
以上就是本文的全部内容 以上就是本文的全部内容, 希望对大家的学习有所帮助, 也希望大家多多支持武林网。