1. 程式人生 > >jsp:session的跟蹤方式

jsp:session的跟蹤方式

建議 tcl tle oid sin [] package des pack

一,HTTP 是一種"無狀態"協議,這意味著每次客戶端檢索網頁時,客戶端打開一個單獨的連接到 Web 服務器,服務器會自動不保留之前客戶端請求的任何記錄。所以需要跟蹤用戶的sessionID來判斷是否是同一個用戶訪問。

1,使用cookie保存sessionID,cookie是將服務器需要保存的信息寫入到本地的文件夾裏保存。可以將session保存到cookie中,對於客戶端的後續請求可以使用接收到的 cookie 來識別。

瀏覽器不支持 cookie,所以建議不要使用這種方式來維持 session 會話。

例:

package com.test;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SessionCookies extends
HttpServlet { /** * Constructor of the object. */ public SessionCookies() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲得session HttpSession ss=request.getSession(); String aa=ss.getId(); PrintWriter out=response.getWriter(); /*out.print(aa);*/ //將session儲存到cookie中 Cookie session=new Cookie("session",aa); Cookie cookie=null; Cookie[] cookies=null; //獲得cookie中的sessionID cookies=request.getCookies(); for(int i=0;i<cookies.length;i++){ cookie=cookies[i]; out.print(cookie.getName()); } } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

2,使用HttpSession跟蹤

Servlet 還提供了 HttpSession 接口,該接口提供了一種跨多個頁面請求或訪問網站時識別用戶以及存儲有關用戶信息的方式。

例:

package com.test;

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 SessionUrl extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public SessionUrl() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    Integer typecount=0;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //獲取session。無則創建新的session
            HttpSession session=request.getSession(true);
            PrintWriter out=response.getWriter();
            //判斷是否是新創建的session
            if(session.isNew()){
                out.print("新的session創建成功");
            }else{
                //如果不是新的session則記錄這個session訪問頁面的次數
                typecount++;
                out.print(typecount);
            }
        
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

jsp:session的跟蹤方式