session

今天学到了session,其为一次访问提供不变量,例如在用户登录的程序中,要保存其用户名和密码

@WebServlet(name = "LoginMgrController",value = "/loginMgr")
public class LoginMgrController extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.处理乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //2.收参
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String inputVcode=request.getParameter("inputVcode");

        //2.1先验证码,只有验证码正确才能继续做用户名,密码处理
        String codes=(String) request.getSession().getAttribute("codes");
        if (!inputVcode.isEmpty()&&inputVcode.equalsIgnoreCase(codes)){
            //3.调用业务方法
            ManagerService managerService=new ManagerServiceImpl();
            Manager mgr=managerService.login(username,password);//传前端的username和password
            //4.处理结果,流程跳转
            if (mgr!=null){
                //success
                //跳转目标方式
                HttpSession session=request.getSession();
                session.setAttribute("mgr",mgr);
                response.sendRedirect("/servletlianxi_war_exploded/showallcontroller");
            }else {
                response.sendRedirect("/servletlianxi_war_exploded/loginMgr.html");
            }
        }else {
            response.sendRedirect("/servletlianxi_war_exploded/loginMgr.html");
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

 

posted @ 2022-04-05 21:09  橙大力  阅读(38)  评论(0)    收藏  举报