码迷,mamicode.com
首页 > 编程语言 > 详细

JavaWeb 使用Session实现一次性验证码

时间:2019-08-28 12:37:34      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:大小写   uppercase   位置   span   请求   asd   getattr   img   矩形   

 

表单

<form action="loginServlet" method="post">
  请输入验证码:<input type="text" name="code" />
  <img src="getCodeServlet" /><br />
  <button type="submit">提交</button>
</form>

载入页面时,会自动请求getCodeServlet,获取图片(验证码)。

 

 

 

getCodeServlet,产生验证码

 1 @WebServlet("/getCodeServlet")
 2 public class GetCodeServlet extends HttpServlet {
 3     //验证码的宽、高
 4     private static int WIDTH=80;
 5     private static int HEIGHT=25;
 6 
 7     //绘制背景
 8     private void drawBg(Graphics g){
 9         //rgb
10         g.setColor(new Color(128, 128, 128));
11         //绘制矩形。x,y,wigth,height
12         g.fillRect(0,0,WIDTH,HEIGHT);
13         //随机绘制100个干扰点
14         Random random=new Random();
15         for (int i=0;i<100;i++){
16             //产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行
17             int x=random.nextInt(WIDTH);
18             int y=random.nextInt(HEIGHT);
19             g.drawOval(x,y,1,1);
20 
21             //干扰点的颜色也可以随机,随机产生red,green,blue即可
22             //g.setColor(new Color(red,green,blue));
23         }
24     }
25 
26 
27     //绘制验证码
28     private void drawCode(Graphics g,char[] code){
29         g.setColor(Color.BLACK);
30         //字体、样式(多个时竖线分隔)、字号
31         g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
32         //在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。
33         g.drawString(code[0]+"",1,17);
34         g.drawString(code[1]+"",16,15);
35         g.drawString(code[2]+"",31,18);
36         g.drawString(code[3]+"",46,16);
37     }
38 
39     //随机产生4位验证码
40     private char[] getCode(){
41         String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
42         char[] code=new char[4];
43         Random random=new Random();
44         for (int i=0;i<4;i++){
45             //[0,62)
46             int index= random.nextInt(62);
47             code[i]=chars.charAt(index);
48         }
49         return code;
50     }
51 
52 
53     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
54         HttpSession session = request.getSession();
55         ServletOutputStream sos = response.getOutputStream();
56         response.setContentType("image/jpeg");
57 
58         //设置浏览器不缓存此图片
59         response.setHeader("Pragma","No-cache");
60         response.setHeader("Cache-Control","no-cache");
61         response.setDateHeader("Expires",0);
62 
63         //创建内存图片
64         BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
65         Graphics g= bufferedImage.getGraphics();
66         char[] code=getCode();
67         //将验证码放到session域中。session对象要在提交响应之前获得
68         session.setAttribute("code",new String(code));
69         drawBg(g);
70         drawCode(g,code);
71         g.dispose();
72 
73         //将图片输出到浏览器
74         ByteArrayOutputStream baos = new ByteArrayOutputStream();
75         ImageIO.write(bufferedImage,"JPEG",baos);
76         baos.writeTo(sos);
77         baos.close();
78         sos.close();
79     }
80 
81     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
82         doPost(request,response);
83     }
84 }

 

 

 

loginServlet,处理表单

 1 @WebServlet("/loginServlet")
 2 public class LoginServlet extends HttpServlet {
 3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 4         response.setContentType("text/html;charset=utf-8");
 5         HttpSession session = request.getSession();
 6         String trueCode= (String) session.getAttribute("code");
 7         String code=request.getParameter("code");
 8 
 9         if (code.equals(trueCode)){
10             response.getWriter().write("验证码正确");
11         }
12         else {
13             response.getWriter().write("验证码错误");
14         }
15     }
16 
17     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
18         doPost(request,response);
19     }
20 }

 

上面的处理方式要区分验证码的大小写。

 

不区分大小写:

//先转换为全大写|全小写,再判断
        trueCode=trueCode.toLowerCase();
        code=code.toLowerCase();

        //trueCode=trueCode.toUpperCase();
        //code=trueCode.toUpperCase();

 

JavaWeb 使用Session实现一次性验证码

标签:大小写   uppercase   位置   span   请求   asd   getattr   img   矩形   

原文地址:https://www.cnblogs.com/chy18883701161/p/11423084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!