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

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

%>

时间: 2024-08-29 20:29:29

session设置获取与应用的相关文章

hibernate中session的获取使用以及其他注意事项

hibernate中session的获取使用以及其他注意事项 前言:工作时,在同时使用Hibernate的getSession().getHibernateTemplate()获取Session后进行数据查询时不是出现了"session is close"异常就是出现其他异常问题,痛定思痛,决定收集并整理相关资料,方便今后的使用. 一.session的获取 在hibernate中的Session对象通过SessionFactory来管理,可以通过使用openSession ().get

记录一次Session偶尔获取不到的解决过程

导读 平台下某子系统有密码登录需求,初步考虑用Session,登录后设置Session[key]=value;Session中若某key对应的Session,即Session[key]为null则限制操作.对应所用技术点:Asp.Net MVC & Razor 视图引擎,技术点较简单,虽然走了一些弯路,但加深了对Session的理解. 一.前台ajax校验输入密码正确性 $.ajax({ url: "@Url.Action("CheckPwd","Earni

JavaScript设置获取和设置属性的方法

这篇文章主要介绍了JavaScript设置获取和设置属性的方法,学会使用getAttribute.setAttribute的用法,需要的朋友可以参考下 getAttribute 该方法用来获取元素的属性,调用方式如下所示: 复制代码代码如下: object.getAttribute(attribute) 以此前介绍的一些方法不同,getAttribute方法不属于document对象,所以不能通过document对象调用.它只能通过元素节点对象来调用. 该方法只接受一个参数,你指定要查询的属性的

jQuery - 设置获取内容和属性

获取选中select :$("#id option:selected").val(); 自定义radio:    $("input[name=sex][value="+data.sex+"]").attr("checked",true); 获取radio:           $("input[name='sex']:checked").val() 设置input不能编辑:$("#cashNum&

JQuery设置获取下拉菜单选项的值 多实例

分享下JQuery如何设置获取下拉菜单某个选项的值,多种方法,值得收藏. JQuery获取和设置Select选项 获取Select :获取select 选中的 text :$(“#ddlRegType”).find(“option:selected”).text();获取select选中的 value:$(“#ddlRegType “).val();获取select选中的索引:$(“#ddlRegType “).get(0).selectedIndex;设置select:设置select 选中的

终端服务器超出最大允许连接数|修改end a disconnected session设置

终端服务器超出最大允许连接数|修改end a disconnected session设置.请问我的WIN2003服务器,由于之前有两个用户登录后并没有正常注销出来,所以当我使用远程桌面远程登录的时候就会提示"超出最大允许",请问我能如何通过远程把那两个用户注销出来? 回答:根据您的描述,我对您提出的问题的理解是:您使用Windows Server 2003远程桌面时候收到"超出最大允许". 如果我的理解有误,请您告诉我. 类似问题发生是因为remote deskt

js,jq设置获取属性,样式

js设置获取属性:设置属性-element.setAttribute("属性名称","属性值"):获取属性-element.getAttribute("属性名称"): jq设置获取属性:attr() js获取行内样式:document.getElementById(“button″).style.width; js获取css文件里样式: IE var width = mydiv.currentStyle['width']: Chorme  var

如何为session设置超时时间?

要为session设置超时时间,首先要清楚怎么样算是超时, 超时时间是session自上一次活跃时间到本次开始活跃时间之间的间隔时间大于设置的session超时时间. 为session设置超时时间常用的方法有三种: 第一种方法: HttpSession session = request.getSession(true); session.setAttribute("user", user); session.setMaxInactiveInterval(30); 注意:单位是秒,设置

jquery - 设置/获取内容和属性

一般我们会遇到给某个元素添加或更改原有的文字: 1. 设置/获取内容 - text().html() 以及 val() 设置内容常用的三个方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - 设置或返回表单字段的值 eg: 通过 text().html() 以及 val() 方法来设置/获取内容: $("#btn1").click(function(){ $("#test1").t