我在javascript中創建了一個cookie,但是當我嘗試在java中獲取值時,我得到了以前的值 - 這個cookie沒有刷新。有什麼方法可以刷新cookie,以便我能得到正確的值? 這是JavaScript:如何獲得在javascript中創建的cookie的java值?
<script>
function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else {
var expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
alert (value);
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function del_cookie(name) {
document.cookie = name +
'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
}
$j(document).ready(function(){
// remove all click-events from header_nav_submenu
$j(".header_nav_submenu").unbind('click');
$j(".header_nav_submenu").click(function(){
var goHref = $j(this).attr('href');
del_cookie("last_page_nav");
alert(getCookie("last_page_nav"));
setCookie("last_page_nav", goHref.substring(7), 7);
alert(getCookie("last_page_nav"));
});
});
</script>
,這就是我如何嘗試從Java中的cookie獲取的價值:
if(FriendlyURLUtil.getFriendlyURL(request.getServerName())==null){
Cookie[] cookies = ((HttpServletRequest) request).getCookies();
for(int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("last_page_nav")) {
System.out.println("The days " + cookies[i].getMaxAge());
System.out.println("The cookie says " + cookies[i].getValue());
((HttpServletResponse) response).sendRedirect(cookies[i].getValue());
}
}
}
我想了解這裏的流程。首先你的客戶端從服務器獲得一個響應(頁面)。然後客戶端運行一些JavaScript來更改存儲在客戶端上的cookie。當客戶端從服務器請求下一頁時,它不會發送新的修改過的cookie,而是發送舊的原始cookie? – 2011-12-21 12:56:44