gpt4 book ai didi

java - httprequest.getsession 返回 null

转载 作者:行者123 更新时间:2023-11-30 08:20:50 24 4
gpt4 key购买 nike

我编写了一个过滤器,它会使当前 session 无效并创建新 session 并将旧 session 的属性复制到新 session 中。这在 tomcat5 和 jdk 1,4 中工作正常但是当我将它切换到 tomcat6 和 jdk 1.6 时过滤器运行后,下一个请求 httprequest.getsession(false )null。为什么它在 tomcat 6 中表现不同。请帮忙。这是代码片段

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException
{
HttpServletRequest httpRequest = (HttpServletRequest) request;
System.out.println("within GenerteNewSessionFilter");
System.out.println("within GenerteNewSessionFilter getRequestURI"+httpRequest.getRequestURI());
System.out.println("within GenerteNewSessionFilter getRequestURL"+httpRequest.getRequestURL());

System.out.println("within GenerteNewSessionFilter session false"+httpRequest.getSession(false));

String reqUrl=httpRequest.getRequestURL().toString();
if (reqUrl==null){
reqUrl="";
}
int idxLogin=reqUrl.indexOf("LoginSubmit.jsp");
int idxReg=reqUrl.indexOf("RegistrationSubmit.jsp");
int idxErr=reqUrl.indexOf("Error");

int idxSave=-1;
System.out.println("within GenerteNewSessionFilter reqUrl "+reqUrl);
System.out.println("within GenerteNewSessionFilter idxLogin index"+idxLogin);
System.out.println("within GenerteNewSessionFilter idxReg index"+idxReg);
System.out.println("within GenerteNewSessionFilter idxErr index"+idxErr);

if (httpRequest.getSession(false) != null && (idxLogin >0 || idxReg >0) && idxErr <0 ){
//copy session attributes from old session to a map.
System.out.println("copy session attributes from old session to a map");
HttpSession session = httpRequest.getSession();

HashMap old=new HashMap();
Enumeration keys = (Enumeration) session.getAttributeNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();

old.put(key, session.getAttribute(key));
session.removeAttribute(key);


}
System.out.println("old session id "+session.getId());

//invalidate session and create new session.
session.invalidate();
//create new session
session = httpRequest.getSession(true);

//copy session attributes from map session to new session
Iterator it = old.entrySet().iterator(); //iterator
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
//putitng old attributes in to new session
session.setAttribute((String) pairs.getKey(), pairs.getValue());

}//end while loop
System.out.println("end copy session attributes");
System.out.println("new session id status "+httpRequest.getSession(false));
System.out.println("final new session session id "+session.getId());
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {


}

}

最佳答案

HttpServletRequest.getSession(boolean create) 的 javadoc明确指出,如果您将 false 值传递给它的参数,它只会返回 HttpSession(如果已经存在)。如果没有与请求关联的 session ,它将返回 null

因此,如果您使当前 session 无效,显然在您的下一个请求中调用 request.getSession(false) 将返回 null

如果您想在使当前 session 无效后创建一个新 session ,请使用:request.getSession(true) 或更简单:request.getSession()

关于java - httprequest.getsession 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25664811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com