session的一些方法

session的一些方法:

package com.stono.servlet.listenerorder;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionON2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取已经存在的session;
        // HttpSession session = req.getSession(false);
        HttpSession session = req.getSession();
        // 用这个方式竟然不能让session失效;
        session.setMaxInactiveInterval(0);
        // 这个session可能为null;
        boolean new1 = session.isNew();
        System.out.println("session is " + new1);
        System.out.println("当前时间:" + new Date());
        long creationTime = session.getCreationTime();
        Date date = new Date(creationTime);
        System.out.println("创建时间:" + date);
        long lastAccessedTime = session.getLastAccessedTime();
        Date date2 = new Date(lastAccessedTime);
        System.out.println("最后访问时间:" + date2);
        int maxInactiveInterval = session.getMaxInactiveInterval();
        System.out.println("最大间隔:" + maxInactiveInterval);
        // 用这个方法使session失效了;
        session.invalidate();
        PrintWriter writer = resp.getWriter();
        writer.println("com.stono.servlet.listenerorder.SessionON2.doGet(HttpServletRequest, HttpServletResponse)");
    }
}