Apache Maven;会话技术

Apache Maven是一个项目管理和构建工具,它基于项目对象模型(POM)的概念,通过一小段描述信息来管理项目的构建、报告和文档。

Maven模型有:

项目对象模型、依赖管理模型、插件


会话技术:

会话:浏览器和服务端进行请求发送和响应的过程

会话跟踪:一种维护浏览器状态的方法,服务器需要识别多次请求是否来自于同一服务器,以便在同一次会话的多次请求之间共享数据。

服务器用来识别浏览器的过程就是会话跟踪。

现在的浏览器和服务器不支持数据共享是因为

浏览器和服务器之间使用的是HTTP请求来进行数据传输

HTTP协议是无状态的,每次浏览器向服务器请求时,服务器都会视为新的请求

HTTP协议设计成无状态的目的是让每次请求之间相互独立,互不影响

请求与请求之间独立后,就无法实现多次请求之间的数据共享

小结:HTTP协议是无状态的,靠HTTP协议无法实现会话跟踪;

           想要实现会话跟踪,需要用到Cookie和Session

Cookie、Session这两个技术都可以实现会话跟踪,最大的区别是:Cookie存储在浏览器端,而Session存储在服务器端。

客户端会话跟踪技术:Cookie

Cookie的操作分为两类——发送Cookie、获取Cookie

发送Cookie:

1.创建Cookie对象,并设置数据
Cookie cookie = new Cookie("key","value");

2.发送Cookie到客户端:使用response对象
response.addCookie(cookie);

@WebServlet("/cookie")
public class CookieServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
        // 发送Cookie
        // 1. 创建Cookie对象
        Cookie cookie = new Cookie("name", "tzp");
        // 2. 使用response对象发送Cookie
        resp.addCookie(cookie);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
        doGet(req, resp);
    }
}
//访问查看Cookie的值为:name=tzp

获取Cookie:

1.获取客户端携带的Cookie,使用request对象

Cookie[ ] cookies = request.getCookies();

2.使用Cookie对象方法获取数据

cookie.getName();

cookie.getValue();

@WebServlet("/getCookie")
public class GetCookieServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
        //获取Cookie
        Cookie[] cookies = req.getCookie;
        //遍历Cookie
        if(cookies!=null){
            for(Cookie cookie:cookies){
                //获取Cookie的名称和值
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println(name + ":" + value);
            }
        } 
    }
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
        doGet(req,resp);
    }
}

在访问CookieServlet和GetCookieServlet的中间把浏览器关闭,GetCookieServlet无法再获取到Cookie的数据;默认情况下Cookie存储在浏览器内存中,当浏览器关闭,内存释放Cookie被销毁

所以遇到的问题是如何将Cookie持久化存储:

setMaxAge(int seconds)这个API可以来完成

参数值为:

1.正数:将Cookie写入浏览器所在电脑的硬盘持久化存储,到时间自动删除

2.负数:默认值,Cookie在当前浏览器内存中,当浏览器关闭,Cookie被销毁

3.零:删除对应Cookie

设置Cookie时间:

Cookie cookie=new Cookie("name","tzp");
cookie.setMaxAge(60*60*24*7); //7天存活时间
resp.addCookie(cookie);

Cookie存储中文:

//发送Cookie 创建Cookie对象
String value="彭于晏";
Cookie cookie=new Cookie("name",URLEncoder.encode(value,StandardCharsets.UTF-8));
resp.addCookie(cookie);
----------------------------------------------------------------------------------

@WebServlet("/getCookie")
Cookie[] cookies=req.getCookies();
if(cookies!=null){
    for(Cookie cookie:cookies){
        String name=cookie.getName();
        String value= URLDecoder.decode(cookie.getValue(),StandardCharsets.UTF_8);
        System.out.println(name+":"+value);
    }
}

服务端会话跟踪技术:Session

存储在客户端的数据容易被窃取和截获,存在很多不安全因素,存储在服务端的数据相比于客户端来说更安全

Session的基本使用:

获取Session对象,使用的是request对象:

HttpSession session = request.getSession();

Session对象提供的功能:

1.存储数据到session域中:

void  setAttribute(String name,Object o)

2.根据key,获取值

Object getAttribute(String name)

3.根据key,删除该键值对

void  removeAttribute(String name)

@WebServlet("/session")
public class SessionServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
        //存储到Session中
        //1.获取Session对象
        HttpSession session = req.getSession();
        //2.存储数据
        session.setAttribute("username","tzp");
    }
     
    @Override
    protected void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
        doGet(req,resp);
    }
}
@WebServlet("/getSession")
public class GetSessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
        // 获取数据,从session中
        // 1. 获取Session对象
        HttpSession session = req.getSession();
        // 2. 获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);
    }

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

先访问/session,将数据存入Session;再访问/getSession,从Session中获取数据

 通过案例的效果,能看到Session能够在一次会话中两次请求之间共享数据。

注意 : Session 中可以存储的是一个 Object 类型的数据,也就是说 Session 中可以存储任意数据类型
Session是基于Cookie来实现的

Cookie和Session的区别:

存储位置: Cookie 是将数据存储在客户端, Session 将数据存储在服务端
安全性: Cookie 不安全, Session 安全
数据大小: Cookie 最大 3KB Session 无大小限制
存储时间: Cookie 可以通过 setMaxAge() 长期存储, Session 默认 30 分钟
服务器性能: Cookie 不占服务器资源, Session 占用服务器资源
应用场景 :
购物车 : 使用 Cookie 来存储
以登录用户的名称展示 : 使用 Session 来存储
记住我功能 : 使用 Cookie 来存储
验证码 : 使用 session 来存储
结论
Cookie 是用来保证用户在未登录情况下的身份识别
Session 是用来保存用户登录后的数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值