当前页面: 开发资料首页 → Java 专题 → JSP实现在浏览器关闭cookies情况下的会话管理
JSP实现在浏览器关闭cookies情况下的会话管理
摘要: JSP实现在浏览器关闭cookies情况下的会话管理
器关闭cookies情况下的会话管理
<link> href="../../css/index.css" rel="stylesheet" type="text/css">
</head>
function valiform() {
if(document.form2.keyword.value==""){
alert("请输入关键字!");
document.form2.keyword.focus();
return false;
}
}
function valiform1(){
if(document.form5.pinjianame.value==""){
alert("请输入姓名!");
document.form5.pinjianame.focus();
return false;
}
if(document.form5.pinjiacontent.value==""){
alert("请输入评价内容!");
document.form5.pinjiacontent.focus();
return false;
}
}
<body bgcolor="#FFFFFF" background="../../images/background.gif" leftmargin="0" topmargin="0">
<table width="756" height="1146" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" class="LTR">
<tr>
<td width="747" height="78"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="19%" height="65"></td>
<td width="81%"><table width="94%" height="53" border="0" align="center" cellpadding="0" cellspacing="0" class="LTR">
<tr>
<td height="53">
</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="20" class="ArticleTeitle"><table width="100%" height="25" border="0" cellpadding="0" cellspacing="0" bgcolor="#3399FF" >
<tr>
<td width="29%">
您的位置〖 大小〖 发布日期〖 浏览
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
通常,会话管理是通过服务器将 Session ID作为一个cookie存储在用户的Web浏览器中来唯一标识每个用户会话。如果浏览器不支持 cookies,或者将浏览器设置为不接受cookies,我们可以通过 URL重写来实现会话管理。实质上URL重写是通过向URL连接添加参数,并把 sessionID作为值包含在连接中。然而,为使这生效,你需要为你的servlet响应部分的每个连接添加 session ID 。
把 session ID 加到一个连接可以使用一对方法来简化:response.encodeURL()使URL包含sessionID,如果你需要使用重定向,可以使用response.encodeRedirectURL()来对URL进行编码。encodeURL()及encodeRedirectedURL()方法首先判断cookies是否被浏览器支持;如果支持,则参数URL被原样返回,sessionID将通过cookies来维持。
来看下面的例子,两个JSP文件:hello1.jsp和hello2.jsp,及它们之间的影响。我们在 hello1.jsp中简单的创建一个会话,并在 session中存储一个对象实例。接着用户可以点击页面的连接到达 hello2.jsp。在 hello2.jsp 中,我们从session中获取原先放置的对象并显示它的内容。注意,我们在 hello1.jsp 中调用了encodeURL()方法来获得 hello2.jsp的链接,使得在浏览器停用cookies的情况下,sessionID自动添加到URL,hello2.jsp仍能得到session对象。
首先在启用 cookies 的情况下运行。然后关闭对cookie的支持,重启浏览器,再运行一次。每次你都可以看到会话管理在起作用,并能在页之间传递信息。
注意,如果你想让这个例子能在关闭了cookies的浏览器中工作,你的JSP引擎必须支持URL重写。
<table width="677" border="0">
<tr>
<td width="392">hello1.jsp
<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
'>hello2.jsp
hello2.jsp
<%@ page session="true" %>
<%
Integer i= (Integer)session.getValue("num");
out.println("Num value in session is "+i.intValue());
%> </td>
<td width="275" valign="top">
</td>
</tr>
</table>
<script>
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: rm播放器与jsp
后一篇: 利用JSP代码(标记)管理您的图像