通过Session案例分析一次性验证码登录
验证码的实现原理:
在一个Servlet中生成验证,并把验证码上的数据保存在Session,用户提交验证码之后,会提交给另外一个Servlet程序。在获取用户提交数据的Servlet中的从Session中把验证码取出,在取出的同时从Session中把验证码删除。
1.注册页面:register.jsp
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%> <%Stringpath=request.getContextPath(); StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>"rel="externalnofollow"> MyJSP'login.jsp'startingpage function_changImg(){ document.getElementById("myimg").src="http://www.haoziyuan.cc/day11/checkImg?"+newDate().getMilliseconds(); }
用户名 * 密码 输入验证码 看不清,换一张 ${imgError}
2.生成验证码参考:cn.itcast.session.CheckImgServlet
publicclassCheckImgServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ intwidth=120; intheight=40; //先生成一张纸 BufferedImagebufi=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); //画笔 Graphicsg=bufi.getGraphics(); //设置背景颜色 //修改画笔颜色 g.setColor(Color.white); //填充 g.fillRect(0,0,width,height); //绘制边框 //设置边框颜色 g.setColor(Color.red); //画边框 g.drawRect(0,0,width-1,height-1); //准备一些数据 Stringdata="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz"; //生成随机对象 Randomr=newRandom(); Stringcheckcode=""; //生成4个随机的数字 for(inti=0;i<4;i++){ //生成随机颜色 g.setColor(newColor(r.nextInt(255),r.nextInt(255),r.nextInt(255))); //设置字体 g.setFont(newFont("宋体",Font.ITALIC,25)); Stringc=data.charAt(r.nextInt(data.length()))+""; g.drawString(c,10+(20*i),30); checkcode+=c; } //将生成的验证码放到session中 request.getSession().setAttribute("session_checkcode",checkcode); //画干扰线 for(inti=0;i<8;i++){ //设置随机颜色 g.setColor(newColor(r.nextInt(255),r.nextInt(255),r.nextInt(255))); //画线两点确定一线 g.drawLine(r.nextInt(width),r.nextInt(height),r.nextInt(width),r.nextInt(height)); } //将图片输出到浏览器 ImageIO.write(bufi,"jpg",response.getOutputStream()); } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ } }
3.验证码的验证参考:cn.itcast.session.RegistServlet
publicclassRegistServletextendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ } publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ //校验验证码的有效性:服务端生成的验证码和表单提交的验证码一致才算有效 //服务端生成的验证码保存在session容器中 //获得session中的验证码 HttpSessionsession=request.getSession(); Stringsession_checkcode=(String)session.getAttribute("session_checkcode"); //使用完后,迅速清除session中验证码的属性 session.removeAttribute("session_checkcode"); //获得表单提交的验证码 Stringform_checkcode=request.getParameter("form_checkcode"); //判断什么是非法如果非法,终止 if(session_checkcode==null||!session_checkcode.equals(form_checkcode)){ //说明验证码错误 request.setAttribute("imgError","验证码错误!"); //将request对象转发给login.jsp request.getRequestDispatcher("/login.jsp").forward(request,response); //结束当前方法 return; } //如果合法,继续校验用户名和密码的有效 Stringusername=request.getParameter("username"); Stringpassword=request.getParameter("password"); } }
以上所述是小编给大家介绍的通过Session案例分析一次性验证码登录问题,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!