Loading

Session是否为新建情况的判断

Session是否为新建情况的判断:

Xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>TestBootWeb</display-name>
    <servlet>
        <servlet-name>TestServlet1</servlet-name>
        <servlet-class>com.stono.servlet.listenerorder.SessionON</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>TestServlet2</servlet-name>
        <servlet-class>com.stono.servlet.listenerorder.SessionON2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet1</servlet-name>
        <url-pattern>/a.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>TestServlet2</servlet-name>
        <url-pattern>/b.do</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

 

Servlet:

package com.stono.servlet.listenerorder;
import java.io.IOException;
import java.io.PrintWriter;
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 SessionON extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        // 如果客户端的cookie禁用,每次获得的session都是新的;在chrome Developer Tools中的Cookies界面可以看到没有request的JSESSIONID;
        // 判断获得的session是否是新的;
        boolean new1 = session.isNew();
        System.out.println("session is " + new1);
        PrintWriter writer = resp.getWriter();
        writer.println("com.stono.servlet.listenerorder.SessionON.doGet(HttpServletRequest, HttpServletResponse)");
        resp.encodeURL("a.do");
    }
}
package com.stono.servlet.listenerorder;
import java.io.IOException;
import java.io.PrintWriter;
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);
        // 这个session可能为null;
        boolean new1 = session.isNew();
        System.out.println("session is " + new1);
        PrintWriter writer = resp.getWriter();
        writer.println("com.stono.servlet.listenerorder.SessionON2.doGet(HttpServletRequest, HttpServletResponse)");
    }
}

 

posted @ 2015-10-08 16:41  stono  阅读(2068)  评论(0编辑  收藏  举报