静态html请求session丢失

[color=brown][size=small]一个临时推广的纯静态html活动页面,与原有项目分离,现活动页面要记录当前报名用户,并给当前报名用户一次抽奖机会,大致情况就是这样,程序如下:[/size][/color]

public boolean checkChestCnts(HttpServletRequest request,Integer activeId,String account){
//当前报名信息写入session
HttpSession session = request.getSession();
session.setAttribute("activeId", activeId);
session.setAttribute("account", account);
Statistics stat = getByActiveIdAndAccount(activeId, account);//查询用户
if(null == stat){
stat = new Statistics();
stat.setActiveId(activeId);
stat.setAccount(account);
stat.setLotteryCnt(1);
statisticsDao.addStatistics(stat);//存入报名信息
return true;
}
return false;
}

/**
*检查并设置报名用户抽奖次数
*/
@RequestMapping("checkChestCnt")
public void checkChestCnts(HttpServletRequest request,HttpServletResponse response,Statistics statistics){
boolean bool = statisticsBo.checkChestCnts(request, statistics.getActiveId(), statistics.getAccount());
String data = statistics.getJsonpCallback()+ "({\"result\":"+bool+"})";
try {
response.getWriter().write(data);
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* 获取当前报名用户可抽奖次数
*/
@RequestMapping("getChestCnt")
public void getChestCnts(HttpServletRequest request,HttpServletResponse response,Statistics statistics){
HttpSession session = request.getSession();
Object accountObj = session.getAttribute("account");
//此处打印accountObj的值是null,checkChestCnt.do请求已先执行
//.............
}

[color=brown][size=small]静态页面用的是ajax的jsonp的方式请求服务端,account也已设置到session当中,在getChestCnt.do的请求中获取不到,当时怀疑getChestCnt.do请求中的session已经不是checkChestCnt.do中设置的那个session,html页面发生两个请求中的session已不在一个回话底下,如是在两个请求的响应头中设置P3P协议就可以成功获取到值[/size][/color]

response.setHeader("P3P","CP=CAO PSA OUR");
//response.setHeader("P3P","CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'");


[color=brown][size=small]这个应该是和Iframe跨域session丢失是一样的问题,欢迎大伙提出不同见解。顺带贴一句网上找到的另一个设置,留作参考 response.setHeader("Access-Control-Allow-Origin" , "*" );//设置为*只是用来解决post请求跨域的问题[/size][/color]

你可能感兴趣的:(笔记)