码迷,mamicode.com
首页 > 其他好文 > 详细

session的一些方法

时间:2015-10-08 18:01:55      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

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)");
    }
}

 

session的一些方法

标签:

原文地址:http://www.cnblogs.com/stono/p/4861572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!