我有一个使用过滤器servlet创建会话的Web应用程序,但是我遇到如下问题:
url1: localhost:8080/home.html
url2: localhost:8080/user/1.html
如果用户首先访问url1
,则会创建会话,如果用户导航到url2
。它不会创建第二个会话。
但如果用户首先访问url2
,则会创建会话,如果用户导航到url2
。它将创建第二个新会话。
它出了什么问题?
如果用户首先访问/
,它会为/user
创建一个JSESSIONID cookie,为url2
创建另一个。
我的代码在这里:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
String errorMsg = null;
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse)
{
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpServletResponse httpResponse = (HttpServletResponse) response;
httpRequest.setCharacterEncoding("UTF-8");
HttpSession httpSession = httpRequest.getSession(false);
String path = httpRequest.getServletPath();
if ("/login.htm".equals(path)){
String mail = httpRequest.getParameter("mail");
String password = httpRequest.getParameter("password");
if ((!Utils.isNull(mail)) && (!Utils.isNull(password)))
{
UserDTO dto = new UserDTO();
dto.setPassword(password);
dto.setMail(mail);
dto = is.loginValidate(dto);
if(dto!=null){
dto.setClientid(httpRequest.getHeader("User-Agent") + httpRequest.getRemoteAddr());
httpSession.setAttribute("system.userinfo", dto);
is.saveLastLoginDate(httpRequest);
}
}
}else{
if(httpSession==null){
httpSession = httpRequest.getSession(true);
}
Object obj = httpSession.getAttribute("system.userinfo");
if(obj==null){
UserDTO dto = new UserDTO();
dto.setUid(GUESTID);
dto.setClientid(httpRequest.getHeader("User-Agent") + httpRequest.getRemoteAddr());
httpSession.setAttribute("system.userinfo", dto);
}
}
答案 0 :(得分:0)
它看起来是一个环境问题,如果我更换另一台电脑,问题就会消失