• 请求转发和重定向实现与Ajax实现表单登陆


    private void login(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String verifycode = request.getParameter("verifycode");
            String machinecode = (String) request.getSession().getAttribute("rand");
    
            if (verifycode.equalsIgnoreCase(machinecode)) {
                // 2.验证用户名和密码
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                System.out.println(username);
                User user = userService.login(username, password);
                if (user != null) {
                    // 登录成功
                    request.getSession().setAttribute("user", user);
                    response.sendRedirect("index.jsp");
                } else {
                    // 登录失败
                    request.setAttribute("msg", "用户名或密码错误");
                    request.getRequestDispatcher("login.jsp").forward(request,
                            response);
                }
    
            } else {
                request.setAttribute("msg", "验证码错误");
                request.getRequestDispatcher("login.jsp")
                        .forward(request, response);
            }
        }
        
        private void login2(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String verifycode = request.getParameter("verifycode");
            String machinecode = (String) request.getSession().getAttribute("rand");
    
            if (verifycode.equalsIgnoreCase(machinecode)) {
                // 2.验证用户名和密码
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                System.out.println(username);
                User user = userService.login(username, password);
                if (user != null) {
                    // 登录成功
                    request.getSession().setAttribute("user", user);
                    PrintWriter out = response.getWriter();
                    out.write(JSON.toJSONString(new MsgVo(1,"登陆成功")));
                } else {
                    // 登录失败
                    PrintWriter out = response.getWriter();
                    out.write(JSON.toJSONString(new MsgVo(2,"用户名和密码错误")));
                }
    
            } else {
                PrintWriter out = response.getWriter();
                out.write(JSON.toJSONString(new MsgVo(3,"验证码错误")));
            }
        }
  • 相关阅读:
    jqGrid详解及高级应用(十四)
    ERP存储过程的调用和树形菜单的加载(四)
    IDisposable接口
    SQL调用系统存储过程整理
    Net作业调度-----Quartz.Net
    C#泛型(二)
    ERP通用存储过程封装(三)
    ERP PowerDesigner工具使用(二)
    ERP简介(一)
    jQuery.TreeView插件实现树状导航(十三)
  • 原文地址:https://www.cnblogs.com/vieta/p/10891525.html
Copyright © 2020-2023  润新知