App(4.25)

登录功能的很多实例都是和Request相关的啊

代码还不能完全懂,尤其是这一段,来自:https://www.jianshu.com/p/8c67aef800b4

public class ActionServlet2 extends HttpServlet {
    
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //1.获取URI
        String uri = request.getRequestURI();
        HttpSession session = request.getSession(); 
        //2.截取URI中的动作
        uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
        if(uri.equals("/login")){
            //判断用户名和密码
            String uname = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            UserDAO udao = new UserDAO(); 
            try{
                //登录认证
                if(udao.auth(uname,pwd)==false){
                    //登录失败
                    //request.setAttribute("msg","用户名或密码不正确");
                    response.getWriter().print("{\"result\":\"fail\",\"message\":\"username or password is wrong\"}");
                }else{
                    //登录成功,记录用户信息到session中
                    session.setAttribute("uname",uname);
                    //session过期时间设置为5分钟
                    session.setMaxInactiveInterval(300);
                    response.getWriter().print("{\"result\":\"success\",\"message\":\"login success\"}");
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

 

App(4.25)

上一篇:安卓强弱指针分析与测试


下一篇:第6章 移动语义和enable_if:6.5 使用Concepts简化enable_if<>表达式