利用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 = 150505032428319459075L;私人最终字体mfont = new font(“ arial black”,font.plain,16);私有最终int int img_width = 100;私人最终int int img_heigth = 18;私有颜色getrandColor(int fc,int bc){random random = new andural();如果(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); } public void Service(httpservletrequest请求,httpservletresponse响应)抛出ServletException,ioexception {wendesp.setheader(“ pragma”,“ not-cache”);响应。 response.setDateHeader(“ Expires”,0); response.setContentType(“ image/jpeg”); bufferedimage映像=新的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);字符串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(); image.write(图像,“ jpeg”,response.getOutputStream()); } private String getRandomChar(){int rand =(int)Math.Round(Math.random() * 2);长ITMP = 0; char ctmp ='/u0000'; switch(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版本=“ 1.0”编码=“ utf-8”?> <web-app xmlns:xsi =“ http://www.w.org/2001/xmlschema-chema-chema-instance” xmlns“ 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>IndustryDemo</display-name> <welcome-file-list> <welcome-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</welcome-file> </welcome-file-list> <servlet> <servlet-name> img </servlet-name> <servlet-class> com.zyc.demo.drewimage </servlet-class> </servlet> </servlet> <servlet-mapping> <servlet-name> img </servlet-name> img </servlet-name> <url-pattern> <url-pattern>/img.do </imimg.do </url-pattern> </url-pattern> </url-pattern> </servlet-</web-mapps>
JSP文件
<%@ page Lagansy =“ java” import =“ java.util。字符串basepath = request.getScheme()+“://”+request.getServername()+“:”+request.getServerport()+path+path+“/”; %> <!doctype html公开” - // W3C // DTD HTML 4.01过渡// en> <html> <html> <head> <base href =“ <%= basepath%> <%> basepath%>”> <Tital>我的JSP jsp'yanzhengma.jsp'yanzhengma.jsp'yanzhengma.jsp'yanzhengma.jsp'起始页面> http-equiv =“ cache-control” content =“ no-cache”> <meta http-equiv =“ expir expires” content =“ 0”> <meta http-equiv =“键words” content =“键word1,键 - type =“ text/css” href =“ styles.css”> - > </head> <body> <img src =“ img.do”> <button onclick =“ window.location.recod.reload();”>刷新</button> </button> </button> </body> </body> </html> </html> </html> </html> </html> </html> </html> </html> </html> </html> </html>
简单实用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。,也希望大家多多支持武林网。