jsp+Servlet编程实现验证码的方法
程序员文章站
2023-08-24 17:08:48
本文实例讲述了jsp+servlet编程实现验证码的方法。分享给大家供大家参考,具体如下:
这里用到两个类,一个用于验证码实现,一个为后台servlet验证输入是否正确:...
本文实例讲述了jsp+servlet编程实现验证码的方法。分享给大家供大家参考,具体如下:
这里用到两个类,一个用于验证码实现,一个为后台servlet验证输入是否正确:
codeutil.java--------验证码具体实现:
package util; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.ioexception; import java.io.outputstream; import java.io.printwriter; import java.util.random; import javax.imageio.imageio; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import javax.servlet.jsp.pagecontext; import javax.servlet.jsp.tagext.bodycontent; import com.sun.image.codec.jpeg.jpegcodec; import com.sun.image.codec.jpeg.jpegimagedecoder; import com.sun.image.codec.jpeg.jpegimageencoder; public class codeutil extends httpservlet { public void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { //system.out.println("哈哈哈"); response.setcontenttype("image/jpeg"); //设置页面不缓存 response.setheader("pragma", "no-cache"); response.setheader("cache-control", "no-cache"); response.setdateheader("expires", 0); //在内存中创建图像 int width = 90; int height = 35; bufferedimage image = new bufferedimage(width,height,bufferedimage.type_int_rgb); //获取图形上下文 graphics g = image.getgraphics(); //随机类 random random = new random(); //设定背景 g.setcolor(getrandcolor(200, 250)); g.fillrect(0, 0, width, height); //设定字体 g.setfont(new font("times new roman",font.plain,30)); //随机产生干扰线 g.setcolor(getrandcolor(160, 200)); for (int i = 0; i < 100; i++) { int x = random.nextint(width); int y = random.nextint(height); int xl = random.nextint(12); int yl = random.nextint(12); g.drawline(x, y, x + xl, y + yl); } //随机产生4位验证码 string[] codes = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; string code = ""; for(int i=0;i<4;i++){ string str = codes[random.nextint(codes.length)]; code += str; // 将认证码显示到图象中 g.setcolor(new color(50 + random.nextint(110), 20 + random.nextint(110), 30 + random.nextint(110))); //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawstring(str, 18 * i +13, 27); //文字间距*i+距离左边距,上边距 } httpsession session=request.getsession(); // 将认证码存入session session.setattribute("code", code); // 图象生效 g.dispose(); // 输出图象到页面 imageio.write(image, "jpeg", response.getoutputstream()); //加上下面代码,运行时才不会出现java.lang.illegalstateexception: getoutputstream() has already been called ..........等异常 response.getoutputstream().flush(); response.getoutputstream().close(); response.flushbuffer(); } //获取随机颜色 private color 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); return new color(r,g,b); } }
checkcodeutil.java-----------用户输入验证功能
package util; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class checkcodeutil extends httpservlet { public void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.setcharacterencoding("utf-8"); response.setcontenttype("text/html;charset=utf-8"); printwriter out = response.getwriter(); //从session获取验证码 httpsession session=request.getsession(); string code=session.getattribute("code").tostring(); system.out.println(code); //获取用户输入验证码 string input=request.getparameter("code"); system.out.println(input); if(code.equalsignorecase(input)){ //转发数据 request.setattribute("result", "true"); request.getrequestdispatcher("test/regist.jsp").forward(request, response); // response.sendredirect(request.getcontextpath()+"/regist/regist.jsp"); }else{ request.setattribute("result", "false"); request.getrequestdispatcher("test/regist.jsp").forward(request, response); } } }
测试页面效果:
测试页面代码:
<%@ page session="true" pageencoding="utf-8" contenttype="text/html; charset=utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> <script> function showresult1(){ alert('输入正确'); } function showresult2(){ alert('输入错误'); } </script> </head> <% response.setcontenttype("text/html;charset=utf-8"); request.setcharacterencoding("utf-8"); string path=request.getcontextpath(); if(request.getattribute("result")!=null){ string result=request.getattribute("result").tostring(); out.print(result); if(result.equals("true")){ out.print("true"); out.print("<script>showresult1();<script>"); }else{ out.print("<script>showresult2();<script>"); } } %> <body> <br>验证码演示 <form action="<%=path %>/checkcodeutil" method="post"> <input type="text" size="10" name="code"/> <img src="<%=path %>/codeutil" id="img"/> <a href="javascript:;" onclick="document. getelementbyid('img').src='<%=path %>/codeutil?'+new date().gettime();">看不清,换一个</a> <input type="submit" value="提交"> </form> </body> </html>
希望本文所述对大家jsp程序设计有所帮助。
上一篇: vue-render渲染
下一篇: 西瓜的吃法推荐,各位小主请笑纳