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();

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

%>

转载于:https://www.cnblogs.com/bonly-ge/p/7040630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值