asp教程.net session 简单测试应用程序
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>session测试</title>
<script type="text/网页特效">
function ajaxget()
{
var ajaxobject = window.activexobject?new activexobject("microsoft.xmlhttp"):new xmlhttprequest();
ajaxobject.onreadystatechange = function ajaxstatechange() {
if(ajaxobject.readystate==4)
{
if(ajaxobject.status==200)
{
var strdata = ajaxobject.responsetext;
if(strdata == 1)
{
window.cleartimeout(auto);
alert(ajaxcount);
}
return;
}
else
{
alert('网络繁忙,请刷新后重试!');
}
}
};
ajaxobject.open('get', 'default.aspx?ajax=1', true);
ajaxobject.setrequestheader("content-type","application/x-www-form-urlencoded;charset=utf-8");
ajaxobject.send();
}
var ajaxcount = 0;
var auto = null;
function startajax()
{
//当通过ajax定时请求aspx页面时,可防止当前页面session丢失!
var btnajax = document.getelementbyid('btnajax');
btnajax.disabled = true;
ajaxcount++;
btnajax.value = ajaxcount;
ajaxget();
auto = window.settimeout(startajax, 1000);
}
</script>
</head>
<body style="font-family:georgia;">
<form id="form1" runat="server">
<input id="btnajax" type="button" value="心 跳" onclick="startajax();" />
<asp:textbox id="textbox1" runat="server"></asp:textbox>
<asp:button id="button1" runat="server" text="添加session" onclick="button1_click" />
<asp:button id="button2" runat="server" text="释放session" onclick="button2_click" />
session包含的值数量:[<%= session.count %>]
<%= session["test"] %>
<div>
当前session数:<%= constants.sessioncount %> <br />
累计session数:<%= constants.sessionallcount%>
</div>
<table border="1" cellpadding="5" style="border-collaps教程e:collapse;">
<tr style="background-color:#dff;">
<th>sessionid</th>
<th>启动时间</th>
</tr>
<% foreach (dictionaryentry r in constants.sessionlist) {
bool iscur = r.key.tostring() == session.sessionid; %>
<tr style="color:<%= iscur?"blue":"black" %>;">
<td><%= r.key %></td>
<td><%= r.value %></td>
</tr>
<% } %>
</table>
</form>
</body>
</html>
cs文件
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections;public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
//注意当ajax请求aspx页面时,aspx页面应设置为不允许缓存!
response.appendheader("pragma", "no-cache");
response.appendheader("cache-control", "no-cache, must-revalidate");
response.appendheader("expires", "0");
if (request.querystring["ajax"] != null)
{
//ajax返回当前网站session数量
response.write(constants.sessionlist.count.tostring());
response.end();
}
}protected void button1_click(object sender, eventargs e)
{
//添加session
//session["test"] = datetime.now;
session.add("test", datetime.now);
response.redirect(request.url.tostring());
}protected void button2_click(object sender, eventargs e)
{
//移除指定session
//session["test"] = null;
//session.remove("test");//移除所有session 与 session.removeall();功能相同
//session.clear();//终止当前回话状态 注意会触发session_end 与session超时效果相同
//调用此句后,sessionid并没有被重置。
//当仅仅调用此句时(当session超时过期时),如果再刷新当前页面则会一直调用session_start 和 session_end
//除非给session添加值或重置sessionid后,才会退出此状态。
session.abandon();//重置当前sessionid
//如果设置的sessionid与已存在的sessionid重复,则将发生session劫持。
//如不存在则会系统会自动创建新session
//response.cookies.add(new httpcookie("asp.net教程_sessionid", this.textbox1.text));
response.redirect(request.url.tostring());
}
}
constanst.cs
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections;/// <summary>
/// constants 的摘要说明
/// </summary>
public class constants
{
public constants()
{
//
// todo: 在此处添加构造函数逻辑
//
}/// <summary>
/// 当前所有session列表
/// </summary>
public static hashtable sessionlist = new hashtable();/// <summary>
/// 当前session个数
/// </summary>
public static int sessioncount = 0;/// <summary>
/// 累计session个数
/// </summary>
public static int sessionallcount = 0;
}global.asax
<%@ application language="c#" %>
<script runat="server">
void application_start(object sender, eventargs e)
{
// 在应用程序启动时运行的代码
}
void application_end(object sender, eventargs e)
{
// 在应用程序关闭时运行的代码}
void application_error(object sender, eventargs e)
{
// 在出现未处理的错误时运行的代码}
void session_start(object sender, eventargs e)
{
constants.sessionallcount++;
constants.sessioncount++;
constants.sessionlist.add(session.sessionid, datetime.now);
//session["sessionstartdatetime"] = datetime.now;
}void session_end(object sender, eventargs e)
{
constants.sessioncount--;
constants.sessionlist.remove(session.sessionid);
}
</script>