其他分享
首页 > 其他分享> > Servlet三大域对象与会话

Servlet三大域对象与会话

作者:互联网

1 servlet域对象

在指定区域内实现数据共享的对象
域对象的统一方法:
   Object getAttribute(String aName);
   void removeAttribute(String aName);
   void setAttribute(String aName,Object vObj);
   Enumeration<String> getAttributeNames();

1 request域对象

requet域对象的共享区域:同一个请求链(请求转发或者请求包含涉及的多个资源)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置request域属性
    request.setAttribute("aa", "aa"+System.currentTimeMillis());
    request.setAttribute("bb", "bb"+System.currentTimeMillis());
    request.getRequestDispatcher("/s0100").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    //获取request域属性
    out.print("request域属性aa="+request.getAttribute("aa")+"<br/>");
    out.print("request域属性bb="+request.getAttribute("bb")+"<br/>");
}

2 session域对象

session:回话: 同一个浏览器对服务器发出的几个连贯(默认时间差不能大于30分钟)的请求
回话跟踪技术:解决http协议的无状态(两次请求之间不能实现数据共享)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置session域属性
    HttpSession session=request.getSession();
    session.setAttribute("aa", "aa_session_"+System.currentTimeMillis());
    session.setAttribute("bb", "bb_session_"+System.currentTimeMillis());
    response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0100'>请求/s0100</a><br/>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    //获取request域属性
    out.print("request域属性aa="+request.getAttribute("aa")+"<br/>");
    out.print("request域属性bb="+request.getAttribute("bb")+"<br/>");
    //获取session域属性
    HttpSession session=request.getSession();
    out.print("session域属性aa="+session.getAttribute("aa")+"<br/>");
    out.print("session域属性bb="+session.getAttribute("bb")+"<br/>");
}

3 application(servletContext)域对象

servletContext上下文对象:同一个项目的所有请求之间实现数据共享
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //获取servletcontext:上下文对象
    ServletContext context=request.getServletContext();
    context=this.getServletContext();
    context=request.getSession().getServletContext();
    //添加servletcontext的域属性
    context.setAttribute("aa", "aa_context_"+System.currentTimeMillis());
    context.setAttribute("bb", "bb_context_"+System.currentTimeMillis());
    response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0100'>请求/s0100</a><br/>");

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    //获取request域属性
    out.print("request域属性aa="+request.getAttribute("aa")+"<br/>");
    out.print("request域属性bb="+request.getAttribute("bb")+"<br/>");
    //获取session域属性
    HttpSession session=request.getSession();
    out.print("session域属性aa="+session.getAttribute("aa")+"<br/>");
    out.print("session域属性bb="+session.getAttribute("bb")+"<br/>");
    //获取servletcontext:上下文对象
    ServletContext context=request.getServletContext();
    out.print("servletContext域属性aa="+context.getAttribute("aa")+"<br/>");
    out.print("servletContext域属性aa="+context.getAttribute("aa")+"<br/>");
}

2 会话跟踪技术之cookie

cookie:由服务器端创建和客户端保存 并通过请求头和响应头传递的键值对对象

代码1:在响应头中添加cookie :::/s0201

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //创建cookie
    Cookie c1=new Cookie("c1", "c1_v");
    Cookie c2=new Cookie("c2", "c2_v");

    //设置cookie的生命周期:
    c1.setMaxAge(-1);//如果值为负数::等价于浏览器内存 浏览器不关闭 cookie一值存在,浏览器关闭 cookie消失
    c1.setMaxAge(0);//如果值为零::删除cookie
    c1.setMaxAge(20);//如果值为正数::cookie保存在硬盘中指定秒::与浏览器内存无关

    //设置cookie的保存目录:默认是当前资源::/java37_ee_06_servlet/s0201
    c1.setPath("/");

    //把cookie添加到响应头 传递给客户端浏览器
    response.setHeader("Set-Cookie", "c3=c3_v");
    response.addCookie(c1);
    response.addCookie(c2);

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    response.getWriter().print("<a href='http://localhost:8080/java37_ee_06_servlet/s0200'>请求/s0200</a><br/>");

}

代码2: 通过请求头获取cookie:::/s0200

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //获取cookie
    Cookie[] arr=request.getCookies();

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    response.getWriter().print("接收到的cookie是:"+Arrays.toString(arr));
}

测试请求/s0201

image

测试请求/s0200

image

原理解释

image

3 会话跟踪技术之session

回话:同一个浏览器发出的几个连贯(默认是30分钟)的请求

代码1:在session中添加数据

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //在session中添加属性
    HttpSession session=request.getSession();
    session.setAttribute("aa", "aa_session_v");
    session.setAttribute("bb", "bb_session_v");

    //session.invalidate();//销毁session
    //session.setMaxInactiveInterval(arg0);//设置session过期间隔 默认是30分钟
    //session.getMaxInactiveInterval();//获取session过期间隔
    // session.setMaxInactiveInterval(10);
    session.getId();//获取sessionid:::当前回话的session对象的ip地址


    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out=response.getWriter();
    out.print("session.getId()="+session.getId()+"<br/>");
    out.print("session.getMaxInactiveInterval()="+session.getMaxInactiveInterval()+"<br/>");
    out.print("<a href='http://localhost:8080/java37_ee_06_servlet/s0200'>请求/s0200</a><br/>");
}

代码2:获取session中的数据

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取cookie
		Cookie[] arr=request.getCookies();
		String message="";
		for (Cookie c : arr) {
			message+=c.getName()+"="+c.getValue()+"<br/>";
		}
		//获取session
		HttpSession session=request.getSession();
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.print("接收到的cookie是:"+message);
		out.print("session域属性aa="+session.getAttribute("aa")+"<br/>");
		out.print("session域属性bb="+session.getAttribute("bb")+"<br/>");
		out.print("session.getId()="+session.getId()+"<br/>");
		out.print("session.getMaxInactiveInterval()="+session.getMaxInactiveInterval()+"<br/>");	
	}

访问/s0202

image

session原理

image

4 cookie和session的区别

参考:https://www.jianshu.com/p/2f7031a69f43

概念

cookie:符合http协议规范的 由服务器创建 客户端保存的 通过请求头和响应头传递的键值对对象
session:由服务器创建和保存 但依赖于名为JSESSIONID的cookie的 用于保存回话数据的对象

相同之处

1 都是解决http无状态的回话跟踪技术
2 都是服务器端创建 都依赖http协议的请求头和响应头

不同之处

1 保存位置不同
   cookie由客户端保存
   session服务器保存
2 默认有效时间不同
   cookie生命周期默认是浏览器内存
   session默认请求间隔是30分钟
3 隐私策略不同
   cookie对客户端可见  不安全
   session对客户端是透明的  不存在敏感信息的安全问题
4 依赖关系
    session依赖名为JSESSIONID的cookie
5 存储容量不同
    cookie单个数据<=4kb 单个站点只能有20个cookie
    sesion没有限制
6 保存的数据格式
    cookie只能保存asicc码信息+二进制
    session可以保存任意数据

标签:三大域,request,与会,session,cookie,print,Servlet,response,out
来源: https://www.cnblogs.com/RenVei/p/15382565.html