📄 286.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 >> How can I enable session tracking for JSP pages if the browser has disabled 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=../list_thread.php?int_attribute=2>精选文章</a>
>> <a href=../list_thread.php?forumid=2&int_attribute=2>Session/Cookie</a>
>> How can I enable session tracking for JSP pages if the browser has disabled cookies? [<a href=../forum/showthread.php?goto=newpost&threadid=286>查看别人的评论</a>]<br>
<hr><p>由 webmaster 发布于: 2001-02-05 16:03</p><p><img src="images/icons/icon10.gif" alt="Talking" border=0> </p><p>We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting.
<br>
<br><font color=red>URL rewriting</font> essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, <font color=red>you need to append the session ID for each and every link </font>that is part of your servlet response.
<br>
<br>Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.
<br>
<br>Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.
<br>
<br>Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other. Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object.
<br>
<br>Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages.
<br>
<br>Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.
<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="../forum/newreply.php?action=newreply&threadid=286">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
[<a href=javascript:void(0) onclick=window.open("../forum/codeVote.php?threadid=286&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("../forum/codeVote.php?threadid=286&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("../forum/codeVote.php?threadid=286&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("../forum/codeVote.php?threadid=286&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="../include/read_thread_script.php?threadid=286"></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 + -