📄 111.html
字号:
<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>精选文章 >> Session/Cookie >> JSP实现在浏览器关闭cookies情况下的会话管理</title>
</head>
<body >
<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>
<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=2>精选文章</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=2&int_attribute=2>Session/Cookie</a>
>> JSP实现在浏览器关闭cookies情况下的会话管理 [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=111>查看别人的评论</a>]<br>
<hr><p>由 webmaster 发布于: 2001-01-21 11:12</p><p> </p><p><br>通常,会话管理是通过服务器将 Session ID 作为一个 cookie 存储在用户的 Web 浏览器中来唯一标识每个用户会话。如果浏览器不支持 cookies,或者将浏览器设置为不接受 cookies,我们可以通过 URL 重写来实现会话管理。 <br><br> 实质上 URL 重写是通过向 URL 连接添加参数,并把 session ID 作为值包含在连接中。然而,为使这生效,你需要为你的 servlet 响应部分的每个连接添加 session ID 。 <br><br> 把 session ID 加到一个连接可以使用一对方法来简化:response.encodeURL() 使 URL 包含 session ID,如果你需要使用重定向,可以使用 response.encodeRedirectURL () 来对 URL 进行编码。 <br><br> encodeURL () 及 encodeRedirectedURL () 方法首先判断 cookies 是否被浏览器支持;如果支持,则参数 URL 被原样返回,session ID 将通过 cookies 来维持。 <br><br> 来看下面的例子,两个 JSP 文件:hello1.jsp 和 hello2.jsp,及它们之间的影响。我们在 hello1.jsp 中简单的创建一个会话,并在 session 中存储一个对象实例。接着用户可以点击页面的连接到达 hello2.jsp。在 hello2.jsp 中,我们从 session 中获取原先放置的对象并显示它的内容。注意,我们在 hello1.jsp 中调用了 encodeURL() 方法来获得 hello2.jsp 的链接,使得在浏览器停用 cookies 的情况下,session ID 自动添加到 URL,hello2.jsp 仍能得到 session 对象。 <br><br> 首先在启用 cookies 的情况下运行。然后关闭对 cookie 的支持,重启浏览器,再运行一次。每次你都可以看到会话管理在起作用,并能在页之间传递信息。 <br><br>注意,如果你想让这个例子能在关闭了 cookies 的浏览器中工作,你的 JSP 引擎必须支持 URL 重写。 <br><br><br>hello1.jsp<br><br><br><%@ page session="true" %><br><%<br>Integer num = new Integer(100);<br>session.putValue("num",num);<br>String url =response.encodeURL("hello2.jsp");<br>%><br><a href='<%=url%>'>hello2.jsp</a><br><br>hello2.jsp<br><br><br><%@ page session="true" %><br><%<br>Integer i= (Integer)session.getValue("num");<br>out.println("Num value in session is "+i.intValue());<br>%><br><br></p></td>
</tr>
</table>
<p>
<CENTER><a href="http://www.jsp001.com/forum/newreply.php?action=newreply&threadid=111">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=111&intVote=4","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>非常多</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=111&intVote=2","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>有一些</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=111&intVote=1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>无帮助</a>](<font color=red>0</font>)
[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=111&intVote=-1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>是灌水</a>](<font color=red>0</font>) </p>
<script language="javascript" src="http://www.jsp001.com/include/read_thread_script.php?threadid=111"></script>
<p><CENTER>
Copyright © 2001 - 2009 JSP001.com . All Rights Reserved <P>
<IMG SRC="../image/jsp001_small_logo.gif" WIDTH="85" HEIGHT="30" BORDER=0 ALT="">
</CENTER></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -