zoukankan      html  css  js  c++  java
  • 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";
                    }                            
                }
                    
            });
        });
  • 相关阅读:
    UNIX网络编程——处理服务器中大量的TIME_WAIT
    UNIX网络编程——套接字选项(心跳检测、绑定地址复用)
    UNIX网络编程——TCP 滑动窗口协议
    [Training Video
    [Training Video
    [Training Video
    [Training Video
    [Training Video
    Use formatter to format your JAVA code
    右键菜单没有新建文本文件了,怎么办?
  • 原文地址:https://www.cnblogs.com/wxwx/p/5481171.html
Copyright © 2011-2022 走看看