我有两个servlet。在第一个servlet,我从数据库中检索用户标识和它存储在这个会话变量:访问两个servlet之间的会话变量
String uId = function.getLogin(username, password); //method getting the id
HttpSession session = request.getSession();
session.setAttribute("userId", uId); // here I'm setting the session variable with the id
现在,在第二个servlet我想要从会话变量,用户id,但java.lang中。正在抛出NullPointerException。
HttpSession session = request.getSession(true);
String uId = session.getAttribute("userId").toString();
int userId = Integer.parseInt(uId); //this is the code that I'm using in the second servlet, and throwing the NullPointerException
我正在做错什么事吗?感谢您的帮助
您可以在投射前检查/打印'uId'的值吗? – Viruzzo 2012-01-16 16:43:45
servlet是否在同一个webapp中?您是否使用相同的浏览器来调用这两个servlet。两次通话之间等待多少时间?为什么你不使用Integer来处理userId,因为它似乎是一个Integer? – 2012-01-16 16:45:39
如果此客户端尚不存在,它只会创建一个新会话。 – 2012-01-16 16:46:34