• 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)");
        }
    }
  • 相关阅读:
    RTP 协议
    RTSP 协议分析 (一)
    RTSP协议分析(二)
    CreateRemoteThread简单应用
    函数开始处的MOV EDI, EDI的作用
    eax,ebx,ecx,edx,esi,edi,ebp,esp寄存器的作用
    C++函数调用原理理解
    WinDBG常用断点命令
    利用bu命令下延迟断点
    Windbg 查看SSDT表
  • 原文地址:https://www.cnblogs.com/stono/p/4861468.html
Copyright © 2020-2023  润新知