java使用servlet实现验证码
利用servlet实现验证码主要继承httpServlet类
packagecom.zyc.demo; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.image.BufferedImage; importjava.io.IOException; importjava.util.Random; importjavax.imageio.ImageIO; importjavax.servlet.ServletException; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; publicclassDrewImageextendsHttpServlet{ /** * */ privatestaticfinallongserialVersionUID=1505032428319459075L; privatefinalFontmFont= newFont("ArialBlack",Font.PLAIN,16); privatefinalintIMG_WIDTH=100; privatefinalintIMG_HEIGTH=18; privateColorgetRandColor(intfc,intbc) { Randomrandom=newRandom(); if(fc>255)fc=255; if(bc>255)bc=255; intr=fc+random.nextInt(bc-fc); intg=fc+random.nextInt(bc-fc); intb=fc+random.nextInt(bc-fc); returnnewColor(r,g,b); } publicvoidservice(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException,IOException { response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); response.setContentType("image/jpeg"); BufferedImageimage=newBufferedImage (IMG_WIDTH,IMG_HEIGTH,BufferedImage.TYPE_INT_RGB); Graphicsg=image.getGraphics(); Randomrandom=newRandom(); g.setColor(getRandColor(200,250)); g.fillRect(1,1,IMG_WIDTH-1,IMG_HEIGTH-1); g.setColor(newColor(102,102,102)); g.drawRect(0,0,IMG_WIDTH-1,IMG_HEIGTH-1); g.setColor(getRandColor(160,200)); for(inti=0;i<30;i++) { intx=random.nextInt(IMG_WIDTH-1); inty=random.nextInt(IMG_HEIGTH-1); intxl=random.nextInt(6)+1; intyl=random.nextInt(12)+1; g.drawLine(x,y,x+xl,y+yl); } g.setColor(getRandColor(160,200)); for(inti=0;i<30;i++) { intx=random.nextInt(IMG_WIDTH-1); inty=random.nextInt(IMG_HEIGTH-1); intxl=random.nextInt(12)+1; intyl=random.nextInt(6)+1; g.drawLine(x,y,x-xl,y-yl); } g.setFont(mFont); StringsRand=""; for(inti=0;i<4;i++) { Stringtmp=getRandomChar(); sRand+=tmp; g.setColor(newColor(20+random.nextInt(110) ,20+random.nextInt(110) ,20+random.nextInt(110))); g.drawString(tmp,15*i+10,15); } HttpSessionsession=request.getSession(true); session.setAttribute("rand",sRand); //System.out.println("写入session"+sRand); g.dispose(); ImageIO.write(image,"JPEG",response.getOutputStream()); } privateStringgetRandomChar() { intrand=(int)Math.round(Math.random()*2); longitmp=0; charctmp='\u0000'; switch(rand) { case1: itmp=Math.round(Math.random()*25+65); ctmp=(char)itmp; returnString.valueOf(ctmp); case2: itmp=Math.round(Math.random()*25+97); ctmp=(char)itmp; returnString.valueOf(ctmp); default: itmp=Math.round(Math.random()*9); returnitmp+""; } } }
下面是web.xml配置
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://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-mapping> <servlet-name>img</servlet-name> <url-pattern>/img.do</url-pattern> </servlet-mapping> </web-app>
jsp文件
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%> <% Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <basehref="<%=basePath%>"> <title>MyJSP'yanzhengma.jsp'startingpage</title> <metahttp-equiv="pragma"content="no-cache"> <metahttp-equiv="cache-control"content="no-cache"> <metahttp-equiv="expires"content="0"> <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> <metahttp-equiv="description"content="Thisismypage"> <!-- <linkrel="stylesheet"type="text/css"href="styles.css"> --> </head> <body> <imgalt="验证码"src="img.do"><buttononclick="window.location.reload();">刷新</button> </body> </html>
简单实用。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。