会话技术Session

1.Session的概念
  • Session是依赖于Cookie的,
  • 每次请求时,会将特殊标识带到服务器端,根据这个标识来找到对应的内存空间,从而实现数据共享!是Servlet规范中四大域对象之一的会话域对象。
  • 作用:是Servlet规范中四大域对象之一的会话域对象。可以实现数据共享
    在这里插入图片描述
2.Session设置共享数据
//Session的基本使用
@WebServlet("/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求字符编码,防止乱码
        req.setCharacterEncoding("utf-8");
        //设置浏览器响应编码[html类型的文本,字符集为utf-8]
        resp.setContentType("text/html;charset=utf-8");
        //1.获取请求的用户名
        String username = req.getParameter("username");
        //2.获取HttpSession的对象
        HttpSession session = req.getSession(true);//默认为true,表示没有session则自动创建
        //[验证是否为同一Session]
        System.out.println(session);
        System.out.println(session.getId());
        //3.将用户名信息添加到共享数据中
        session.setAttribute("username",username);

        //2.解决Cookie禁用方法2:重写url"<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemo1</a>"
       // resp.getWriter().write("<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemoO</a>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

3.Session获取共享数据
//Session获取共享数据[同一个session]
@WebServlet("/ServletDemo2")
public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置浏览器响应编码[html类型的文本,字符集为utf-8]
        resp.setContentType("text/html;charset=utf-8");
        //1.获取HttpSession对象
        HttpSession session = req.getSession();
        //[验证是否为同一Session]
        System.out.println(session);
        System.out.println(session.getId());
        //2.获取共享数据
        String username = (String) session.getAttribute("username");
        //3.将响应数据给浏览器
        resp.getWriter().write(username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

4.Cookie的禁用解决方法
//Cookie的禁用
@WebServlet("/ServletDemo3")
public class ServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解决方法1:提示
        HttpSession session = req.getSession(false);
        System.out.println(session);
        if (session==null){
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().write("为了不影响使用,请不要禁用浏览器的Cookie");
        }
        //2.解决方法2:重写url"<a 【需到第一个界面重写】href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemo1</a>"
       // resp.getWriter().write("<a href='"+resp.encodeURL("http://localhost:8080/Session/ServletDemo3")+"'>go servletDemoO</a>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陪雨岁岁年年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值