[英]How to handle servlet session for different url?
I have a web application which uses a filter servlet to create session, but I have such problem with session as below: 我有一个使用过滤器servlet创建会话的Web应用程序,但是会话存在以下问题:
url1: localhost:8080/home.html
url2: localhost:8080/user/1.html
If the user access the url1
first, it will create a session, and if user navigation to url2
. 如果用户首先访问
url1
,它将创建一个会话,并且如果用户导航到url2
。 it will not create second session. 它不会创建第二个会话。
But if user access the url2
first, it will create a session, and if user navigation to url2
. 但是,如果用户首先访问
url2
,它将创建一个会话,并且如果用户导航到url2
。 it will create second new session. 它将创建第二个新会话。
What's wrong with it? 它出什么问题了?
It looks it creates one JSESSIONID cookie for /
and another for /user
if user access url2
first. 看起来它为
/
创建了一个JSESSIONID cookie,如果用户首先访问url2
则为/user
创建了另一个。
My code is here: 我的代码在这里:
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);
}
}
看来这是环境问题,如果我换了另一台电脑,问题就消失了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.