访问两个 servlet 之间的 Session 变量
我有两个 servlet。在第一个 servlet 中,我从数据库中检索 userId 并将其存储在该会话变量中:
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 中,我想从会话变量中检索该 userId,但抛出了 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
请问我做错了什么吗?感谢您的帮助
I have two servlets. In the first servlet I'm retrieving the userId from the database and store it in this session variable:
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
Now in the second servlet I want to retrieve that userId from the session variable, but a java.lang.NullPointerException is being thrown.
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
Am I'm doing something wrong please? Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该检查异常,它应该为您提供 NPE 的行号。就您的代码而言:
请求可能为空。这是极不可能的。
会话可能为空。这意味着客户端不会发回会话 cookie,或者您可能有多个前端,而另一个前端记录了会话。我们需要更多信息才能找出问题所在。
也可能是
session.getAttribute("userId")
返回 null。我想说这是最有可能的。也许是调用session.setAttribute("userId", uId);
的不同会话。或者,您对function.getLogin(username,password);
的初始调用可能返回 null,因此您在会话中设置null
?你错了。当我阅读 Java 1.6 代码时,
parseInt
永远不会抛出 NPE。这是该方法的第一行:我敢打赌您的会话有问题。我建议使用调试器来找出设置不正确的内容。 Printf 调试也会有所帮助。
You should check the exception which should give you the line number of the NPE. In terms of your code:
Request could be null. This is highly unlikely.
The session could be null. This means that the client is not sending back the session cookie or maybe you have multiple frontends and another one recorded the session. We would need more information to be able to figure out what the problem is there.
It could also be that
session.getAttribute("userId")
is returning null. I'd say this is most likely. Maybe it is a different session where thesession.setAttribute("userId", uId);
was called. Or maybe your initial call tofunction.getLogin(username, password);
returned null so you were settingnull
in the session?You are incorrect. As I read the Java 1.6 code,
parseInt
will never throw NPE. Here's the first line in that method:I bet there is something wrong with your session. I would suggest using the debugger to figure out what is set incorrectly. Printf-debugging would also help.
在设置属性时,您必须确保在此之前没有写入任何响应。此外,之后写入响应(也不例外)。
In setting the attribute you must ensure that upto then no response is written. Furthermore that after that the response is written (no exception).