Session

 

 

 

 

 

 

 

Session还可以通过setAttribute()保存用户信息

 

req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type","text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        HttpSession session = req.getSession();
        session.setAttribute("name",new Person("郭江龙",1));
        String sessionId= session.getId();
        if (session.isNew()){
            writer.write("session已经创建创建成功"+sessionId);
        }else {writer.write("session已经在服务器中存在了 ID:"+sessionId);
        }
 req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setHeader("Content-type","text/html;charset=utf-8");
        HttpSession session = req.getSession();
        Person name = (Person) session.getAttribute("name");
        System.out.println(name.toString());

 

posted on 2023-01-14 17:47  大风吹过12138  阅读(72)  评论(0)    收藏  举报

导航