手机扫码查看
2020javaweb教程之servlet状态管理总结
1 Cookie:
客户端状态管理,保存一小段信息,第一次访问时服务器,服务器cookie响应给浏览器,下次再访问服务器时,把cookie回传给服务器,服务器收到cookie。
Cookie cookie=new Cookie(“xxx”,”141241232″);
cookie.setMaxAge(); 默认-1 保存在内存中, 0 表示失效 正数 过期时间 单位是秒
cookie.setPath();设置有效路径 cookie.setPath(“/“)
response.addCookie(cookie);
2 session:
服务器端状态管理,是一个map集合
session使用sessionid区别:
服务器会把SessionId作为cookie的值发送给浏览器,浏览器再次访问服务器时,服务器读取到cookie(sessionid)。
保存登录信息,购物车
HttpSession session=request.getSession();
3 ServletContext :servlet上下文:是所有用户共享的区域,是一个map集合。
3.1 获取ServletContext :
this.getServletContext();
request.getServletContext();
3.2 作用
request.getServletContext.getRealPath();//获取项目的发布路径
request.getServletContext.getServerInfo();//服务器信息
request.getServletContext.getContextPath();//上下文路径 项目路径
request.getContextPath();//同上
3.3 容器使用
<context-param>
<param-name></param-name>
<param-value></param-value>
</context-param>
servletContext.getInitparamter(“name”);
3.4 配置ServletContext启动参数 web.xml
request.getServletContext.getInitparamenter(“appname”);



发表回复