フィルター サーブレットを使用してセッションを作成する Web アプリケーションがありますが、次のようなセッションの問題があります。
url1: localhost:8080/home.html
url2: localhost:8080/user/1.html
ユーザーが最初にアクセスするurl1
とセッションが作成され、ユーザーが に移動するとurl2
. 2 番目のセッションは作成されません。
ただし、ユーザーがurl2
最初にアクセスすると、セッションが作成され、ユーザーがurl2
. 2 番目の新しいセッションが作成されます。
どうしたの?
ユーザーが最初にアクセスした場合に、 1 つの JSESSIONID Cookie/
と別のJSESSIONID Cookie を作成するようです。/user
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);
}
}