• session设置获取与应用


    1. Session的判断写入与读取

    设置全局变量Num并且判断是否为新建,如果为新建计数累加

    <%!int Num = 0;%>

    <%

    if (session.isNew()) {

    Num += 1;

    session.setAttribute("Num", Num);//将Num变量值存入session

    }

    %>

    <font color="blue">您是第</font>

    <font color="red"><%=session.getAttribute("Num")%></font>

    <font color="blue">个访问本站的用户</font>

     

    在后台处理数据的时候,把用户数据写入session

    <%

    String username= request.getParameter("username");

    String sex= request.getParameter("password");

    session.setAttribute("username", username);

    session.setAttribute("password ", password);

    %>

     

    获取后台传过来的session数据

    <%

    Object id = session.getAttribute("username");

    Object password = session.getAttribute("password ");

    if (id != null) {

    out.print("姓名:" + id.toString());

    out.print(" <br/>");

    out.print("密码:" + password.toString());

    } else {

    out.print("无设置session数据!!");

    }

    %>

     

    Session其他属性值的获取方法

    <table border="1">

    <tr>

    <th align="left">session的建立时间</th>

    <td><%=session.getCreationTime()%></td>

    </tr>

    <tr>

    <th align="left">session的标识符串</th>

    <td><%=session.getId()%></td>

    </tr>

    <tr>

    <th align="left">session最后被请求的时间</th>

    <td><%=session.getLastAccessedTime()%></td>

    </tr>

    <tr>

    <th align="left">session预设结束的时间</th>

    <td><%=session.getMaxInactiveInterval()%></td>

    </tr>

    <%

    session.setMaxInactiveInterval(session.getMaxInactiveInterval()+100);

    %>

    <tr>

    <th align="left">session新的有效时间</th>

    <td><%=session.getMaxInactiveInterval()%></td>

    </tr>

    <tr>

    <th align="left">是否为新建的session</th>

    <td><%=session.isNew()%></td>

    </tr>

    <tr>

    <th align="left">session1 对象取值</th>

    <td><%=session.getAttribute("user1")%></td>

    </tr>

    <tr>

    <th align="left">session2 对象取值</th>

    <td><%=session.getAttribute("user2")%></td>

    </tr>

    </table>

    <%

    session.removeAttribute("user1");

    // session.invalidate();

    //使无效,这里用这行代码,会出现重新打开浏览器或者通过超链接都是新用户,统计会出现错误。

    %>

  • 相关阅读:
    上周热点回顾(12.10-12.16)团队
    上周热点回顾(12.3-12.9)团队
    上周热点回顾(11.26-12.2)团队
    上周热点回顾(11.19-11.25)团队
    上周热点回顾(11.12-11.18)团队
    【故障公告】SendCloud 邮件发送服务故障造成大量 QQ 邮箱收不到邮件团队
    上周热点回顾(11.5-11.11)团队
    上周热点回顾(10.29-11.4)团队
    SPSS学习系列之SPSS Modeler的帮助文档怎么调出来使用?
    SPSS学习系列之SPSS Text Analytics是什么?
  • 原文地址:https://www.cnblogs.com/bonly-ge/p/7040630.html
Copyright © 2020-2023  润新知