• session+验证码 学习


     分析

    LoginServlet类
    @WebServlet("/loginServlet")
    public class LoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置request编码
            request.setCharacterEncoding("utf-8");
            //获取参数
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String checkcode = request.getParameter("checkcode");
    
            //判断验证码是否正确
            HttpSession session = request.getSession();
            //获取验证码的值
            String checkCode_session = (String) session.getAttribute("checkCode");
            //一获取完,就立马删除,以保证验证码的一次性
            session.removeAttribute("checkCode");
            if (checkCode_session!=null&&checkCode_session.equalsIgnoreCase(checkcode)){//忽略大小写比较
                //验证码正确
                //1.判断验证码和密码是否一致
                if ("zhangsan".equals(username)&&"123".equals(password)){//完善是需要调用userDao查询数据库的
                    //登陆成功
                    //1.存储用户信息
                    session.setAttribute("user",username);
                    //2.重定向success.jsp
                    response.sendRedirect(request.getContextPath()+"/success.jsp");
    
    
                }else {
                    //登陆失败
                    //1.存储信息到request域中
                    request.setAttribute("login_error","用户名或密码 不正确");
                    //2.转发
                    request.getRequestDispatcher("/login.jsp").forward(request,response);
                }
    
            }else {
                //验证码不正确
                //1.存储信息到request域中
                request.setAttribute("cc_error","验证码不正确");
                //2.转发
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }

    验证码类Servlet

    @WebServlet("/checkCodeServlet")
    public class CheckCodeServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            int width=100;
            int height=50;
            BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
            Graphics g = image.getGraphics();
            g.setColor(Color.green);
            g.drawRect(0,0,width,height);
            g.setColor(Color.yellow);
            g.fillRect(0,0,width-1,height-1);
    
            String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            Random random=new Random();
            g.setColor(Color.magenta);
    
            StringBuilder sb = new StringBuilder();
            for (int i=1;i<=4;i++){
                int index = random.nextInt(str.length());
                char ch=str.charAt(index);
                g.drawString(ch+"",i*width/5,height/2);
                sb.append(ch);
            }
            String checkCode_session = sb.toString();
            HttpSession session=request.getSession();
            session.setAttribute("checkCode",checkCode_session);
    
            System.out.println("验证码是   :  " + checkCode_session);
    
            //画干扰线
            g.setColor(Color.green);
            for (int i = 0; i < 16; i++) {
                int x1=random.nextInt(width);
                int x2=random.nextInt(width);
                int y1=random.nextInt(height);
                int y2=random.nextInt(height);
                g.drawLine(x1,y1,x2,y2);
            }
            ImageIO.write(image,"jpg",response.getOutputStream());
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }

    login.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>login</title>
        <script>
            window.onload=function () {
                var img = document.getElementById('img');
                img.onclick=function () {
                    var date=new Date().getTime();
                    // img.src="/session/checkCodeServlet?"+date
                    this.src="/session/checkCodeServlet?time="+date
                }
            }
        </script>
        <style>
            div{
                color: red;
            }
        </style>
    </head>
    <body>
        <form action="/session/loginServlet">
            <table>
                <tr>
                    <td>用户名</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td>密码</td>
                    <td><input type="password" name="password"></td>
                </tr>
                <tr>
                    <td>验证码</td>
                    <td><input type="text" name="checkcode"></td>
                </tr>
                <tr>
                    <td colspan="2"><img id="img" src="/session/checkCodeServlet"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="登录"></td>
                </tr>
            </table>
        </form>
    
        <div><%= request.getAttribute("login_error")==null?"":request.getAttribute("login_error")%></div>
        <div><%= request.getAttribute("cc_error")==null?"":request.getAttribute("cc_error")%></div>
    
    </body>
    </html>
    View Code

     success.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登录成功</title>
    </head>
    <body>
    
    欢迎登录,<h1><%=request.getSession().getAttribute("user") %></h1>
    </body>
    </html>
    View Code
  • 相关阅读:
    char*”类型的值不能用于初始化“LPTSTR , Const char*”类型的值不能用于初始化“LPCTSTR
    LPCTSTR和LPTSTR和char *
    C++ char*,const char*,string的相互转换(转)
    vs2017 开发 C++ 操作mysql的动态库
    VS2017 创建C++Dll动态库(二)
    VS2017中托管C++程序调用托管C++生成的动态库,程序无法调试的问题(转)
    win10 MySQLroot 远程连接
    c++中c_str()的用法详解(转)
    【605】Python的开发环境相关 (不同版本python、pip)
    【604】Python class __dict__.update的使用
  • 原文地址:https://www.cnblogs.com/shiguanzui/p/11725882.html
Copyright © 2020-2023  润新知