Servlet第五篇(会话技术之Session)

Session

什么是Session
  Session 是另一种记录浏览器状态的机制。不同的是Cookie保存在浏览器中,Session保存在服务器中。用户使用浏览器访问服务器的时候,服务器把用户的信息以某种的形式记录在服务器,这就是Session
为什么要使用Session技术?
  Session比Cookie使用方便,Session可以解决Cookie解决不了的事情【Session可以存储对象,Cookie只能存储字符串。】。
Session API
  long getCreationTime();【获取Session被创建时间】
  String getId();【获取Session的id】
  long getLastAccessedTime();【返回Session最后活跃的时间】
  ServletContext getServletContext();【获取ServletContext对象】
  void setMaxInactiveInterval(int var1);【设置Session超时时间】
  int getMaxInactiveInterval();【获取Session超时时间】
  Object getAttribute(String var1);【获取Session属性】
  Enumeration getAttributeNames();【获取Session所有的属性名】
  void setAttribute(String var1, Object var2);【设置Session属性】
  void removeAttribute(String var1);【移除Session属性】
  void invalidate();【销毁该Session】
  boolean isNew();【该Session是否为新的】
Session作为域对象
  Session作为一种记录浏览器状态的机制,只要Session对象没有被销毁,Servlet之间就可以通过Session对象实现通讯
  一般来讲,当我们要存进的是用户级别的数据就用Session,那什么是用户级别呢?只要浏览器不关闭,希望数据还在,就使用Session来保存。
Session的生命周期和有效期
  Session在用户第一次访问服务器Servlet,jsp等动态资源就会被自动创建,Session对象保存在内存里,这也就为什么上面的例子可以直接使用request对象获取得到Session对象。
  如果访问HTML,IMAGE等静态资源Session不会被创建。
  Session生成后,只要用户继续访问,服务器就会更新Session的最后访问时间,无论是否对Session进行读写,服务器都会认为Session活跃了一次。
  由于会有越来越多的用户访问服务器,因此Session也会越来越多。为了防止内存溢出,服务器会把长时间没有活跃的Session从内存中删除,这个时间也就是Session的超时时间。
  Session的超时时间默认是30分钟,有三种方式可以对Session的超时时间进行修改
    第一种方式:在tomcat/conf/web.xml文件中设置,所有的WEB应用都有效
    第二种方式:在单个的web.xml文件中设置,对单个web应用有效
    第三种方式:通过setMaxInactiveInterval()方法设置
使用Session完成简单的购物功能

1、显示书籍并添加购买的处理数据超链接

response.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter=response.getWriter();
        printWriter.write("网页上所有的书籍"+"<br/>");
        
        LinkedHashMap<String,Book> linkedHashMap=DB.getAll();
        Set<Map.Entry<String, Book>> entry=linkedHashMap.entrySet();
        
        for(Map.Entry<String, Book> stringBookEntry:entry){
            Book book=stringBookEntry.getValue();
            String url="/firstServlet/Demo?id="+book.getId();
            printWriter.write(book.getName());
            printWriter.write("<a href='"+url+"'>购买</a>");
            printWriter.write("<br/>");
        }

  2、处理数据(添加book到list并判断是否为空进行第一次初始化)

     response.setContentType("text/html;charset=UTF-8");
        String id=request.getParameter("id");
        Book book=(Book)DB.getAll().get(id);
        
        HttpSession httpSession=request.getSession();
        
        List<Book> list=(List<Book>)httpSession.getAttribute("list");
        
        if(list==null){
            list=new ArrayList<Book>();
            httpSession.setAttribute("list", list);
        }
        list.add(book);
        
        String ur1="/firstServlet/History";
        response.sendRedirect(ur1);

  3、重定向显示购物记录页面(与直接在处理数据页面显示独立起来,且区别在链接id没有明文显示)

     response.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter=response.getWriter();
        HttpSession httpSession =request.getSession();
        List<Book> list=(List<Book>) httpSession.getAttribute("list");
        
        if(list==null||list.size()==0){
            printWriter.write("对不起,您还没有购买任何商品");
        }else{
            printWriter.write("您购买过以下商品");
            printWriter.write("<br/>");
            for(Book book:list){
                printWriter.write(book.getName());
                printWriter.write("<br/>");
            }
        }

 

posted on 2019-03-18 09:43  诗悦网络内推有问必答  阅读(576)  评论(0编辑  收藏  举报

导航