菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
34
0

App(4.25)

原创
05/13 14:22
阅读数 5523

登录功能的很多实例都是和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();
            }
        }
    }
}

 

发表评论

0/200
34 点赞
0 评论
收藏