0

フィルター サーブレットを使用してセッションを作成する Web アプリケーションがありますが、次のようなセッションの問題があります。

url1: localhost:8080/home.html

url2: localhost:8080/user/1.html

ユーザーが最初にアクセスするurl1とセッションが作成され、ユーザーが に移動するとurl2. 2 番目のセッションは作成されません。

ただし、ユーザーがurl2最初にアクセスすると、セッションが作成され、ユーザーがurl2. 2 番目の新しいセッションが作成されます。

どうしたの?

ユーザーが最初にアクセスした場合に、 1 つの JSESSIONID Cookie/と別のJSESSIONID Cookie を作成するようです。/userurl2

私のコードはここにあります:

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);
      }
  }
4

1 に答える 1

0

環境問題のようです。別のPCを変更すると、問題は解決しました。

于 2012-11-16T07:11:21.997 に答える