zoukankan      html  css  js  c++  java
  • 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();

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

    %>

  • 相关阅读:
    Distinct Substrings(spoj 694)
    Musical Theme
    Milk Patterns(poj 3261)
    Repeated Substrings(UVAlive 6869)
    喵星球上的点名(bzoj 2754)
    滑雪与时间胶囊(bzoj 2753)
    莫比乌斯函数之和(51nod 1244)
    欧拉函数之和(51nod 1239)
    数表(bzoj 3529)
    欧拉函数模板
  • 原文地址:https://www.cnblogs.com/bonly-ge/p/7040630.html
Copyright © 2011-2022 走看看