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";
                }                            
            }
                
        });
    });

 

posted @ 2016-05-11 11:07  等等等风来  阅读(97934)  评论(1编辑  收藏  举报