带图形验证码的登录

 1 <%--
 2   User: 丁帅帅
 3   Date: 21/06/03
 4   Time: 14:43
 5   To change this template use File | Settings | File Templates.
 6 --%>
 7 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 8 <html>
 9 <head>
10     <title>带图形验证码的登录</title>
11     <!-- 刷新函数 -->
12     <script>
13         function refresh(){
14             document.getElementById("img1").src="checkcode";
15         }
16         //onclick="mes.innerHTML=''"
17     </script>
18 </head>
19 <body>
20 <%request.setCharacterEncoding("UTF-8"); %>
21 <form method="post" name="form1">
22     用户名<input type="text" name="userid"  value="${param.userid}"/><br>
23     密码<input type="password" name="userpwd" value="${param.userpwd}"/><br>
24     验证码<input type="text" name="checkcode"/>
25     <img border="0" src="checkcode" id="img1"/>
26     <input type="submit" value="换一张" onclick="refresh()"/><br>
27     <input type="submit" value="登录" onclick="form1.action='logcheck'"/>
28     <input type="reset" value="重置"/>
29     <div id="mes">${info} </div>
30 </form>
31 
32 </body>
33 </html>
 1 package com.ding.servlet;
 2 
 3 import javax.imageio.ImageIO;
 4 import javax.servlet.ServletOutputStream;
 5 import javax.servlet.http.HttpSession;
 6 import java.awt.*;
 7 import java.awt.image.BufferedImage;
 8 import java.io.ByteArrayOutputStream;
 9 import java.io.IOException;
10 
11 /**
12  * @Description TODO
13  * @Author 丁帅帅
14  * @Date 21/06/03 14:36
15  * @Version 1.0
16  */
17 @javax.servlet.annotation.WebServlet("/checkcode")
18 public class CheckCode extends javax.servlet.http.HttpServlet {
19     protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
20         response.setContentType("image/jpeg");
21         HttpSession session = request.getSession();
22         int width = 60;
23         int height = 20;
24         // 设置浏览器不要缓存此图片
25         response.setHeader("Pragma", "No-cache");
26         response.setHeader("Cache-Control", "no-cache");
27         response.setDateHeader("Expires", 0);
28         // 创建内存图像并获得其图形上下文
29         BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
30         Graphics g = image.getGraphics();
31         // 产生随机验证码
32         // 定义验证码的字符表
33         String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
34         char[] rands = new char[4];
35         for (int i = 0; i < 4; i++) {
36             int rand = (int) (Math.random() * 36);
37             rands[i] = chars.charAt(rand);
38         }
39         // 产生图像
40         // 画背景
41         g.setColor(new Color(0xDCDCDC));
42         g.fillRect(0, 0, width, height);
43         // 随机产生120个干扰点
44         for (int i = 0; i < 120; i++) {
45             int x = (int) (Math.random() * width);
46             int y = (int) (Math.random() * height);
47             int red = (int) (Math.random() * 255);
48             int green = (int) (Math.random() * 255);
49             int blue = (int) (Math.random() * 255);
50             g.setColor(new Color(red, green, blue));
51             g.drawOval(x, y, 1, 0);
52         }
53         g.setColor(Color.BLACK);
54         g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
55         // 在不同的高度上输出验证码的不同字符
56         g.drawString("" + rands[0], 1, 17);
57         g.drawString("" + rands[1], 16, 15);
58         g.drawString("" + rands[2], 31, 18);
59         g.drawString("" + rands[3], 46, 16);
60         g.dispose();
61         // 将图像输出到客户端
62         ServletOutputStream sos = response.getOutputStream();
63         ByteArrayOutputStream baos = new ByteArrayOutputStream();
64         ImageIO.write(image, "JPEG", baos);
65         byte[] buffer = baos.toByteArray();
66         response.setContentLength(buffer.length);
67         sos.write(buffer);
68         baos.close();
69         sos.close();
70         // 将验证码放到 session 中
71         session.setAttribute("checkCode", new String(rands));
72 
73     }
74 
75     protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
76         doPost(request, response);
77     }
78 }
 1 package com.ding.servlet;
 2 
 3 import javax.servlet.RequestDispatcher;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 import javax.servlet.http.HttpSession;
10 import java.io.IOException;
11 
12 /**
13  * @Description TODO
14  * @Author 丁帅帅
15  * @Date 21/06/03 14:40
16  * @Version 1.0
17  */
18 @WebServlet("/logcheck")
19 public class LogCheck extends HttpServlet {
20     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         request.setCharacterEncoding("UTF-8");
22         String userid = request.getParameter("userid");
23         String userpwd = request.getParameter("userpwd");
24         String usercheckcode = request.getParameter("checkcode");
25         String info = "";
26         HttpSession session = request.getSession();
27         String servercheckcode = (String) session.getAttribute("checkCode");
28         if (!servercheckcode.equalsIgnoreCase(usercheckcode)) {
29             info = "验证码不正确,请重新输入";
30         } else if ("张三".equals(userid) && "123".equals(userpwd)) {
31             info = "登录成功";
32         } else {
33             info = "用户名或密码不正确";
34         }
35 
36         request.setAttribute("info", info);
37         RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
38         rd.forward(request, response);
39 
40     }
41 
42     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43         doPost(request, response);
44     }
45 }

 

posted @ 2021-06-03 15:49  丁帅帅dss  阅读(243)  评论(0)    收藏  举报