java web验证码实现代码分享
程序员文章站
2024-03-12 15:11:56
验证码的作用:通常的登录或者注册系统时,都会要求用户输入验证码,以此区别用户行为和计算机程序行为,目的是有人防止恶意注册、暴力破解密码等。
实现验证码的思路:用 se...
验证码的作用:通常的登录或者注册系统时,都会要求用户输入验证码,以此区别用户行为和计算机程序行为,目的是有人防止恶意注册、暴力破解密码等。
实现验证码的思路:用 server 实现随机生成数字和字母组成图片的功能,用 jsp 页面实现显示验证码和用户输入验证码的功能,再用 server 类分别获取图片和用户输入的数据,判断两个数据是否一致。
代码实现
1.编写数字、英文随机生成的 server 类,源码:
package com; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.printwriter; import javax.imageio.imageio; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class logcheck extends httpservlet { public logcheck() { super(); } public void destroy() { super.destroy(); } public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } /*实现的核心代码*/ public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("image/jpeg"); httpsession session=request.getsession(); int width=60; int height=20; //设置浏览器不要缓存此图片 response.setheader("pragma", "no-cache"); response.setheader("cache-control", "no-cache"); response.setdateheader("expires", 0); //创建内存图像并获得图形上下文 bufferedimage image=new bufferedimage(width, height,bufferedimage.type_int_rgb); graphics g=image.getgraphics(); /* * 产生随机验证码 * 定义验证码的字符表 */ string chars="0123456789abcdefghijklmnopqrstuvwxyz"; char[] rands=new char[4]; for(int i=0;i<4;i++){ int rand=(int) (math.random() *36); rands[i]=chars.charat(rand); } /* * 产生图像 * 画背景 */ g.setcolor(new color(0xdcdcdc)); g.fillrect(0, 0, width, height); /* * 随机产生120个干扰点 */ for(int i=0;i<120;i++){ int x=(int)(math.random()*width); int y=(int)(math.random()*height); int red=(int)(math.random()*255); int green=(int)(math.random()*255); int blue=(int)(math.random()*255); g.setcolor(new color(red,green,blue)); g.drawoval(x, y, 1, 0); } g.setcolor(color.black); g.setfont(new font(null, font.italic|font.bold,18)); //在不同高度输出验证码的不同字符 g.drawstring(""+rands[0], 1, 17); g.drawstring(""+rands[1], 16, 15); g.drawstring(""+rands[2], 31, 18); g.drawstring(""+rands[3], 46, 16); g.dispose(); //将图像传到客户端 servletoutputstream sos=response.getoutputstream(); bytearrayoutputstream baos=new bytearrayoutputstream(); imageio.write(image, "jpeg", baos); byte[] buffer=baos.tobytearray(); response.setcontentlength(buffer.length); sos.write(buffer); baos.close(); sos.close(); session.setattribute("checkcode", new string(rands)); } public void init() throws servletexception { // put your code here } }
2.用于显示验证码的页面:
<%@ 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>index</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="yanzheng" method="post"> <input type="text" name="name" size="5" maxlength="4"> <a href="index.jsp"><img border="0" src="logcheck"></a><br><br>      <input type="submit" value="提交"> </form> </body> </html>
3.用于检验输入的验证码是否正确:
package com; import java.io.ioexception; import java.io.printwriter; import javax.jms.session; 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 yanzheng extends httpservlet { public yanzheng() { super(); } public void destroy() { super.destroy(); } public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } /*核心代码*/ public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string info=null; /*获取输入的值*/ string value1=request.getparameter("name"); /*获取图片的值*/ httpsession session=request.getsession(); string value2=(string)session.getattribute("checkcode"); /*对比两个值(字母不区分大小写)*/ if(value2.equalsignorecase(value1)){ info="验证码输入正确"; }else{ info="验证码输入错误"; } system.out.println(info); request.setattribute("info", info); request.getrequestdispatcher("/login.jsp").forward(request, response); } public void init() throws servletexception { // put your code here } }
4.显示输入结构界面(输入验证码是否正确):
<%@ 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>my jsp 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%=request.getattribute("info") %> </body> </html>
5.项目结构、效果截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 并发编程1
下一篇: new一个对象将会发生的事情