日期:2014-05-16  浏览次数:20285 次

head first 学习笔记 JSP&Servlet--8
1. 会话的销毁
  1.1 设置会话的超时时间
        <session-config>
                <session-timeout>30</session-timeout>
            </session-config>
          单位为分
  
      1.2 在代码进行设置程序的超时时间

           session.setMaxInactiveInterval(20*60); 单位是秒


    2. 若一个session已经销毁了,结果就无法调用其中的属性,否则会报无效状态异常

     情况一:
           resp.setContentType("html/text");
        PrintWriter pWriter = resp.getWriter();
        HttpSession session = req.getSession();
       
        session.setAttribute("foo", "42");
        session.setMaxInactiveInterval(0);
        if (session.isNew())
        {
            pWriter.println("this is a new session!");
        }
        else
        {
            pWriter.println("welcome back!");
        }
       
        pWriter.println("Foo" + session.getAttribute("foo"));

    情况二:
      
         resp.setContentType("html/text");
       PrintWriter pWriter = resp.getWriter();
       HttpSession session = req.getSession();
       
       session.setAttribute("foo", "42");
       session.setAttribute("bar", "420");
       session.invalidate();
       String foo = (String)session.getAttribute("foo");
       pWriter.println("Foo"+foo);

   3. 会话的迁移

      上下文、servletConfig、session ,在从一个JVN迁移到另一个JVM时候,上下文和servletConfig会进行复制,session会迁移。及在分布式系统中,只会保持一份session

     即session从一个VM钝化,从另一个VM激活。

  
 
  4. jsp的编译过程
    
     myjsp.jsp --> myjso_jsp.java --> myjso_jsp.class --> myjsp_jsp (servlet)
   
        
   4.1 scriptlet代码:

    <html><body>
        <% out.printLn(Count.getCount());%>
     </body></html>

    4.2 表达式代码:

      <html><body>
        <%= Count.getCount()%>
     </body></html>
   
    4.3 第三种方式
       <html><body>
        <%! int a = 1 >
        <%= Count.getCount()%>
&nbs