首页 技术 正文
技术 2022年11月21日
0 收藏 497 点赞 2,291 浏览 3396 个字

Tomcat中获取资源文件:

ServletContext().getRealPath(/WEB-INF/classes/db.properties);//获取资源文件的在服务器中的绝对路径

ServletContext()getResourceAsStream() 得到资源文件,返回的是输入流

ServletContext().getRealPath

会话管理

  1.定义:管理浏览器和服务器间会话期间产生的会话数据

  2.域对象:实现资源之间数据的共享

  3.会话数据

    a) Cookie:会话数据保存在浏览器客户端

    1. 使用流程:

        1.Cookie对象在服务端被创建new Cookie(name,value,)->

        2.发送到客户端 respond.addCookie(cookie)(这里隐式发了一个set-Cookie的响应头set-Cookie:name=value)->

        3.浏览器得到这个Cookie会保存起来下次访问服务器时会带着Cookie信息(隐式带着一个Cookie:name=value的请求头)->

        4.服务器获得浏览器发来的Cookie对象request.getCookies()

    1.  主要方法

        构造:Cookie(String ,value);

        设置:setMaxAge(int expiry)//设置cookie的有效时间

          setValue(String newValue);//设置新的Cookie值

           setPath(String path);//设置有效的访问路径

        发生:addCookie(cookie);

        服务端接受:request.getCookies();

    1. 注意:

        Cookie只能保存非中文字符串类型,可以保存多个多个Cookie,一个浏览器可以保存300个Cookie,每个站点只能存20个,每个Cookie大小限制4kb;

        setMaxAge(int expiry):正数:保存在浏览器客户端硬盘时间;

       负数:保存在浏览器内存中,关闭浏览器就没有了

        零:删除同名的Cookie数据

    1.  例子:保存用户上次访问的时间
/** * 案例-用户上次访问时间 * @author APPle * */public class HistServlet extends HttpServlet {/** * */private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");//获取当前时间SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String curTime = format.format(new Date());//取得cookieCookie[] cookies = request.getCookies();String lastTime = null;if(cookies!=null){for (Cookie cookie : cookies) {if(cookie.getName().equals("lastTime")){//有lastTime的cookie,已经是第n次访问lastTime = cookie.getValue();//上次访问的时间//第n次访问//1.把上次显示时间显示到浏览器response.getWriter().write("欢迎回来,你上次访问的时间为:"+lastTime+",当前时间为:"+curTime);//2.更新cookiecookie.setValue(curTime);cookie.setMaxAge(1*30*24*60*60);//3.把更新后的cookie发送到浏览器response.addCookie(cookie);break;}}}/** * 第一次访问(没有cookie 或 有cookie,但没有名为lastTime的cookie) */if(cookies==null || lastTime==null){//1.显示当前时间到浏览器response.getWriter().write("你是首次访问本网站,当前时间为:"+curTime);//2.创建Cookie对象Cookie cookie = new Cookie("lastTime",curTime);cookie.setMaxAge(1*30*24*60*60);//保存一个月//3.把cookie发送到浏览器保存response.addCookie(cookie);}}}

b) Session:会话数据保存在服务端

  1. 出现原因:

    a) Cookie有自己缺点,最大4kb,只能非中文限制多,Session就能解决这个问题,可以存对象,任何类型数据

    b) 服务器能够识别不同浏览器,数据存到哪个域就必须从那个域中或获得对象

  1. 使用流程:

    不同的浏览器request对象获得不同的域对象

    HttpSession session=request.getSession();只要解析他的流程就可以了

      a) 第一次一个浏览器创建Session域对象,服务器给这个session对象分配了一个JSESSIONID对象,用来唯一标识这个session对象的-》

      b) 再将这个JSESSIONID发送到客户端respond.addCookie(“JSESSIONID”, sessionID)->

      c) 之后浏览器发送请求到服务器带着JSESSIONID请求头从服务器中找配备的session对象-》

      d) 找到了就获得这个对象没有找到就重复a)

  1. 主要方法

     a) 创建:request.getSession([true]);

     b) 设置:setMaxInactiveInterval(int interval)//设置保存时间

         Invalidate();//销毁这个session对象

          getId();//获得这个Session的唯一标识JSESSIONID

  1. 注意:

     a)  销毁时间设置:默认情况30min,setMaxInactiveInterval(int interval);设置时间;全局设置销毁时间可以在web.xml中配置

      <!– 修改session全局有效时间:分钟 –>

      <session-config>

      <session-timeout>1</session-timeout>

      </session-config>

      ;手动销毁:invalidate()

    b)  如何避免浏览器的JSESSIONID的cookie随着浏览器关闭而丢失的问题

      手动设置浏览器的Cookie时间

      Cookie cookie=new Cookie();

      cookie.setMaxAge(60*60);

      response.addCookie(cookie);

  1. 例子:
//1.得到session对象HttpSession session = request.getSession(false);if(session==null){System.out.println("没有找到对应的sessino对象");return;}/** * 得到session编号 */System.out.println("id="+session.getId());//2.取出数据String name = (String)session.getAttribute("name");System.out.println("name="+name);
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894