0
点赞
收藏
分享

微信扫一扫

Seeion相关


存储会话数据有两种方式:
Cookie
1) 存储在浏览器端,通过服务器发送cookie数据
2) 使用cookie存储会话数据,相对不安全(可以cookie查到一些用户安全)
3) 从存储数据类型来看,cookie只能存储String类型的数据,
4) 存储数据大小有限制…
Session(用户登录案例)

1) 存储在服务器端的,占用的服务器的资源
2) 存储在服务器端(Session对象,JSESSIONID),相对是安全的
3) 可以存储任意类型的数据
4) 存储读数据无限制

Sesison:
本身在HttpServletRequest对象中就存储在一个方法:
HttpSession getSession():创建或者获取session对象

可以作为域对象,
        保存数据:setAttribute(“数据名称”,”数据的内容”)
        获取数据:getAttribute(“数据名称”) ;

public class SeissonDemo extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //创建Session对象
        HttpSession session = request.getSession(true);
        //发送数据
        response.setContentType("text/html;charset=utf-8");
        session.setAttribute("name", "蓉");

        //非空判断
        if(session!=null){
            //获取数据发送浏览器
            String value = (String)session.getAttribute("name");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("cookie数值为:"+value);
        }

        //删除Session对象
        session.invalidate();
        System.out.println("session对象已删除");

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

}


举报

相关推荐

0 条评论