[Jweb] Session -- SessionInfoServlet.java / ShowSession.java / URLSession.java

来源:互联网 发布:心脏病 知乎 编辑:程序博客网 时间:2024/05/03 02:36

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\">\n" + "<H1 ALIGN=\"CENTER\">"                + heading                + "</H1>\n"                + "<H2>Information on Your Session:</H2>\n"                + "<TABLE BORDER=1 ALIGN=\"CENTER\">\n"                + "<TR BGCOLOR=\"#FFAD00\">\n"                + "  <TH>Info Type<TH>Value\n"                + "<TR>\n"                + "  <TD>ID\n"                + "  <TD>"                + session.getId()                + "\n"                + "<TR>\n"                + "  <TD>Creation Time\n"                + "  <TD>"                + new Date(session.getCreationTime())                + "\n"                + "<TR>\n"                + "  <TD>Time of Last Access\n"                + "  <TD>"                + new Date(session.getLastAccessedTime())                + "\n"                + "<TR>\n"                + "  <TD>Number of Previous Accesses\n"                + "  <TD>"                + accessCount + "\n" + "</TABLE>\n" + "</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>\n"+ "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);}}


0 0