Java Web之验证码
今天来模拟一下验证码,我们需要三个文件,两个Servlet,一个jsp
直接贴代码吧
RandomCodeServlet:主要负责生产验证码
package com.vae.RandomCode; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; import java.util.UUID; @WebServlet("/randomcode") public class RandomCodeServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //生成随机数 String randomCode= UUID.randomUUID().toString().substring(0,5); //把随机数放进session,作为对比的凭证 req.getSession().setAttribute("RANDOM_IN_SESSION",randomCode); //创建图片对象 int width=80; int height=40; int imageType=BufferedImage.TYPE_INT_RGB; BufferedImage image=new BufferedImage(width,height,imageType); //画板 Graphics g=image.getGraphics(); g.setColor(Color.YELLOW); //绘制一个实心的矩形 g.fillRect(1,1,width-2,height-2); //把随机数画进图片中 g.setColor(Color.BLACK); Font font=new Font("宋体",Font.BOLD+Font.ITALIC,20); g.setFont(font); g.drawString(randomCode,10,20); //干扰线,这个的目的就是为了防止黑客去用软件识别验证码 g.setColor(Color.GRAY); Random r=new Random(); for (int i = 0; i < 100; i++) { g.fillRect(r.nextInt(width),r.nextInt(height),2,2); } //关闭 g.dispose(); //把图片对象以流的方式保存出去 ImageIO.write(image,"jpg",resp.getOutputStream()); } }
LoginServlet:主要负责接收JSP传来的参数,验证码和用户输入的验证码进行一个对比
package com.vae.RandomCode; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/randomlogin") public class LoginServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.接收参数 String name=req.getParameter("username"); String email=req.getParameter("email"); String randomcode=req.getParameter("randomcode"); //2.获取session中的随机数,判断验证码是否正确 String randomcodesession=req.getSession().getAttribute("RANDOM_IN_SESSION").toString(); if (!randomcode.equalsIgnoreCase(randomcodesession)) { req.setAttribute("errogMsg","验证码不对,请重新输入"); req.getRequestDispatcher("/randomcode/randomlogin.jsp").forward(req,resp); return; } System.out.println("验证码正确"); req.getSession().removeAttribute("RANDOM_IN_SESSION"); //删除缓存 } }
最后一个是我们的JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>验证码登录</title> </head> <body> <h3>用户登录</h3> ${errogMsg} <form action="/randomlogin" method="post"> 账号:<input type="text" name="username" id="username" required/><br> 邮箱:<input type="email" name="email" id="email" required><br> 验证码:<input type="text" name="randomcode" id="randomcode" size="5" maxlength="5"> <img src="/randomcode" id="randomcodeImg" title="看不清,换一张" style="cursor: pointer;" onclick="change()" > <input type="submit" value="注册"> </form> <script type="text/javascript"> function change() { //刷新验证码的图片 document.getElementById("randomcodeImg").src="/randomcode?"+new Date().getTime(); } </script> </body> </html>
看看效果
就这么简单,但是有一个问题啊,就是表单重复提交了怎么办?我们不能表单重复提交,这个问题下篇文章专门来介绍一下