zoukankan      html  css  js  c++  java
  • JSP内置对象Session

    1

    创建和获取客户的会话

    setAttribute()与getAttribute()

    session.setAttribute(String name , Object obj)

    如session.setAttribute("username" , "陈帝求")

    将一个字符串"陈帝求"放置到session对象中,并且变量名叫username

    session.getAttribute(String name) 该函数返回的是一个Object,是一个对象。

    例子

    String user = (String)session.getAttribute("username");

    String user1= session.getAttribute("username").toString();

    2

    从会话中移除指定对象

    session.removeAttribute(String name);

    例如session.removeAttribute("username");

    3

    设置session有效时间

    因为服务器都是给客户端在服务器端创建30分钟的session,所以必须设置有效时间来释放没有必要的会话

    session.setMaxInactiveInterval(int time);

    如session.setMaxInactiveInterval(3600); //设置了3600秒 就是一个小时的有效时间

    4

    session销毁

    session.invalidate();

    5

    应用session对象实现用户登录

    服务器需要用session来记录客户端的登录的状态,都是通过session来记录用户状态

    JSP内置对象Session

    1

    index.jsp创建一个基本的登录页面 action="deal.jsp"

    <body>
    <form name="form1" method="post" action="deal.jsp">
    用户名: <input name="username" type="text" id="name" style=" 120px"><br>
    密&nbsp;&nbsp;码: <input name="pwd" type="password" id="pwd" style=" 120px"> <br>
    <br>
    <input type="submit" name="Submit" value="登录">
    </form>

    </body>

    2

    deal.jsp中创建了判断标准,我预先设置了3个2维数组,在没有数据库的情况下,先将就一下吧

    <%
    String[][] userList={{"cdq","123"},{"sss","111"},{"aaa","111"}}; //定义一个保存用户列表的二维组
    boolean flag=false;                         //登录状态
    request.setCharacterEncoding("GB18030"); //设置编码
    String username=request.getParameter("username"); //获取用户名
    String pwd=request.getParameter("pwd"); //获取密码
    for(int i=0;i<userList.length;i++)

     if(userList[i][0].equals(username))
     { //判断用户名
      if(userList[i][1].equals(pwd))
      { //判断密码
       flag=true; //表示登录成功
       break;//跳出for循环
      }
     }
    }
    if(flag){ //如果值为true,表示登录成功
     session.setAttribute("username",username);//保存用户名到session范围的变量中
     response.sendRedirect("main.jsp"); //跳转到主页
    }else{
     response.sendRedirect("index.jsp"); //跳转到用户登录页面
    }
    %>

    3 main.jsp

    <%
    String username=(String)session.getAttribute("username"); //获取保存在session范围内的用户名
    %>

    JSP内置对象Session

    <body>
    您好![<%=username %>]欢迎您访问!<br>
    <a href="exit.jsp">[退出]</a>
    </body>

    JSP内置对象Session


    4

    exit.jsp

    <%
    session.invalidate();//销毁session
    response.sendRedirect("index.jsp");//重定向页面到index.jsp
    %>

  • 相关阅读:
    四,iptables详解(4):iptables匹配条件总结之一
    三,iptables详解(3):iptables规则管理
    二,iptables详解(2):iptables实际操作之规则查询
    一,Iptables详解(1):iptables概念
    运维安全之Linux网络安全(iptables)
    【577】U-Net相关
    【576】Python遍历文件夹内所有文件
    【575】连续卷积层(神经网络中的通道 channel)
    【574】KDE与正态分布
    点云投影为深度图
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5075897.html
Copyright © 2011-2022 走看看