session的使用方法及實例


  Session是什么呢?簡單來說就是服務器給客戶端的一個編號。當一台WWW服務器運行時,可能有若干個用戶瀏覽正在運正在這台服務器上的網站。當每個用戶首次與這台WWW服務器建立連接時,他就與這個服務器建立了一個Session,同服務器會自動為其分配一個SessionID,用以標識這個用戶的唯一身份。這個SessionID是由WWW服務器隨機產生的一個由24個字符組成的字符串,我們會在下面的實驗中見到它的實際樣子。


  這個唯一的SessionID是有很大的實際意義的。當一個用戶提交了表單時,瀏覽器會將用戶的SessionID自動附加在HTTP頭信息中,(這是瀏覽器的自動功能,用戶不會察覺到),當服務器處理完這個表單后,將結果
返回給SessionID所對應的用戶。試想,如果沒有SessionID,當有兩個用戶同時進行注冊時,服務器怎樣才能知道到底是哪個用戶提交了哪個表單呢。除了SessionID,在每個Session中還包含很多其他信息。

 

1.設置session

java里面,可以給session添加自定義key,value(HttpServletRequest request 作為方法的輸入參數)

HttpSession session = request.getSession();
session.setAttribute("usrid", userid);

2.取得session

jsp里面可以   這段來源自CSDN一個討論貼,自己時間后並沒有成功,報錯是session is undifiened,后來又找了資料說 javascript不提供訪問session的功能。session只能通過動態程序操作,可以使用ajax給javascript返回值。

session.getAttribute("username");

java里面可以 (HttpServletRequest request 作為方法的輸入參數)

HttpSession session = request.getSession(); 
session.getAttribute("usrname");

 

一個使用session進行超時訪問控制的實例

(1)LoginServlet.java 在登錄時,設置session屬性

 public void doPost(HttpServletRequest request, HttpServletResponse response)  
            throws IOException, ServletException {  
         
        String userid = request.getParameter("username");
        String pwd = request.getParameter("password");
        
        JSONObject json = new JSONObject();  
        
        AdminDAO adminDAO = new AdminDAO();
        List<Admin> userList = adminDAO.findByProperty("usrid", userid);

        if(userList.get(0).getPassword().equals(pwd)){
             json.put("success", true);
             HttpSession session = request.getSession();
             session.setAttribute("usrid", userid);
        } else {
             json.put("success", false);
             json.put("meg", "sorry");
        }          
                 
        PrintWriter pw = response.getWriter();   
        pw.print(json.toString());  
        pw.close();  
    }  

 (2)HomePage.java 在跳轉到相關頁面時,獲取並判斷session

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        JSONObject json = new JSONObject();  
       
        HttpSession session = request.getSession(); 
        if(session==null||session.getAttribute("usrid")==null)
        {
             json.put("success", false);
             json.put("meg", "Time out,please login again!");
        }
        else
        {
           ...
            json.put("jsonArray", array);     
        }    
        
           
        PrintWriter pw = response.getWriter();   
        pw.print(json.toString());  
        pw.close();
    }

 (3)homePage.html 頁面根據(2)的返回值判斷是否執行跳轉操作

$(document).ready(function(){
            $.ajax({
                url: "HomePageServlet",
                type: "post",
                dataType: "json",
                success: function(data) {        
                if (data["success"]) {
                      ...
                } 
                else
                {
                    alert(data["meg"]);
                    window.location.href="login.html";
                }                            
            }
                
        });
    });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM