zoukankan      html  css  js  c++  java
  • [Jweb] Session -- SessionInfoServlet.java / ShowSession.java / URLSession.java

    Session,  Session 是一个篮子

     只要是同一套有父子关系的窗口,都可以访问到同一个 Session。
      启发 :默认 session 是依赖 cookie 的。若cookie完全禁用后解决方案 : 重写URL。
      新浪邮箱 : 做学问不严谨。不像IBM等这样的大公司。
      + "<a href=" + response.encodeURL(request.getRequestURL().toString()) + ">test</a><BR>" 
    + "<a href=" + request.getRequestURI().toString() + ">test</a>" 
       china-pub买书,例如 : 最近浏览的列表,一般是写cookie里面了。

     

    Session , Session 是一个篮子

      只要是同一套有父子关系的窗口,都可以访问到同一个 Session。
      启发 :默认 session 是依赖 cookie 的。若cookie完全禁用后解决方案 : 重写URL。
      新浪邮箱 : 做学问不严谨。不像IBM等这样的大公司。
      + "<a href=" + response.encodeURL(request.getRequestURL().toString()) + ">test</a><BR>" 
    + "<a href=" + request.getRequestURI().toString() + ">test</a>" 

       china-pub买书,例如 : 最近浏览的列表,一般是写cookie里面了。

    SessionInfoServlet.java 

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    
    /**
     * 用于演示Servlet API中的Session管理机制
     */
    public class SessionInfoServlet extends HttpServlet {
        /**
         * Builds an HTML document containing session information and returns it to
         * the client.
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // get current session or, if necessary, create a new one
            HttpSession mySession = request.getSession(true);
    
            // MIME type to return is HTML
            response.setContentType("text/html");
    
            // get a handle to the output stream
            PrintWriter out = response.getWriter();
    
            // generate HTML document
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("<TITLE>Session Info Servlet</TITLE>");
            out.println("</HEAD>");
            out.println("<BODY>");
            out.println("<H3>Session Information</H3>");
            out.println("New Session: " + mySession.isNew());
            out.println("<BR>Session ID: " + mySession.getId());
            out.println("<BR>Session Creation Time: "
                    + new java.util.Date(mySession.getCreationTime()));
            out.println("<BR>Session Last Accessed Time: "
                    + new java.util.Date(mySession.getLastAccessedTime()));
    
            out.println("<H3>Request Information</H3>");
            out.println("Session ID from Request: "
                    + request.getRequestedSessionId());
            out.println("<BR>Session ID via Cookie: "
                    + request.isRequestedSessionIdFromCookie());
            out.println("<BR>Session ID via rewritten URL: "
                    + request.isRequestedSessionIdFromURL());
            out.println("<BR>Valid Session ID: "
                    + request.isRequestedSessionIdValid());
    
            out.println("</BODY></HTML>");
    
            out.close(); // close output stream
        }
    
        /**
         * Returns a brief description of this servlet.
         * 
         * @return Brief description of servlet
         */
        public String getServletInfo() {
            return "Servlet returns session information.";
        }
    }
    --ShowSession.java
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    // Session 追踪
    
    public class ShowSession extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String title = "Session Tracking Example";
            HttpSession session = request.getSession(true); // 在服务器端找不到则创建一个,这是 true 的含义,客户端带不回来
            String heading;
            // Use getAttribute instead of getValue in version 2.2.
            Integer accessCount = (Integer) session.getAttribute("accessCount"); // 计数的功能
            if (accessCount == null) {
                accessCount = new Integer(0);
                heading = "Welcome, Newcomer";
            } else {
                heading = "Welcome Back";
                accessCount = new Integer(accessCount.intValue() + 1);
            }
            // Use setAttribute instead of putValue in version 2.2.
            session.setAttribute("accessCount", accessCount);
    
            out.println("<html><head><title>Session追踪</title></head>"
                    + "<BODY BGCOLOR="#FDF5E6">
    " + "<H1 ALIGN="CENTER">"
                    + heading
                    + "</H1>
    "
                    + "<H2>Information on Your Session:</H2>
    "
                    + "<TABLE BORDER=1 ALIGN="CENTER">
    "
                    + "<TR BGCOLOR="#FFAD00">
    "
                    + "  <TH>Info Type<TH>Value
    "
                    + "<TR>
    "
                    + "  <TD>ID
    "
                    + "  <TD>"
                    + session.getId()
                    + "
    "
                    + "<TR>
    "
                    + "  <TD>Creation Time
    "
                    + "  <TD>"
                    + new Date(session.getCreationTime())
                    + "
    "
                    + "<TR>
    "
                    + "  <TD>Time of Last Access
    "
                    + "  <TD>"
                    + new Date(session.getLastAccessedTime())
                    + "
    "
                    + "<TR>
    "
                    + "  <TD>Number of Previous Accesses
    "
                    + "  <TD>"
                    + accessCount + "
    " + "</TABLE>
    " + "</BODY></HTML>");
    
        }
    
        /** Handle GET and POST requests identically. */
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }
    
    URLSession.java
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.net.*;
    import java.util.*;
    
    //Session追踪
    
    public class URLSession extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		
    		HttpSession session = request.getSession(true);
    
    		out.println("<html><head><title>Session追踪</title></head>"
    				+ "<BODY>
    "
    				+ "session id:" + session.getId() + "<br>"
    				+ "from url:" + request.isRequestedSessionIdFromURL() + "<br>"
    				+ "from cookie:" + request.isRequestedSessionIdFromCookie() + "<br>" 
    				+ "<a href=" + response.encodeURL(request.getRequestURL().toString()) + ">test</a><BR>" 
    				+ "<a href=" + request.getRequestURI().toString() + ">test</a>" 
    				+ "</BODY></HTML>");
    
    	}
    
    	/** Handle GET and POST requests identically. */
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    


  • 相关阅读:
    fianl关键字和static关键字
    jdk的安装
    this关键字和super关键字
    Java 数组
    网络通信知识复习
    Linux 下执行本目录的可执行文件(命令)为什么需要在文件名前加“./”
    CentOS 7 下安装 teamviewer 13
    我的 Putty 配色方案
    在 Mac OS X 下,如何向 sudoers 文件添加新用户
    Windows 7 下使用 pandoc 转换文档格式
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786869.html
Copyright © 2011-2022 走看看