访问两个 servlet 之间的 Session 变量

发布于 2024-12-27 10:15:36 字数 669 浏览 2 评论 0原文

我有两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2025-01-03 10:15:36

您应该检查异常,它应该为您提供 NPE 的行号。就您的代码而言:

HttpSession session = request.getSession(true);

请求可能为空。这是极不可能的。

String uId = session.getAttribute("userId").toString();

会话可能为空。这意味着客户端不会发回会话 cookie,或者您可能有多个前端,而另一个前端记录了会话。我们需要更多信息才能找出问题所在。

也可能是 session.getAttribute("userId") 返回 null。我想说这是最有可能的。也许是调用 session.setAttribute("userId", uId); 的不同会话。或者,您对 function.getLogin(username,password); 的初始调用可能返回 null,因此您在会话中设置 null

// this is the code that [... is] throwing the NullPointerException
int userId = Integer.parseInt(uId); 

你错了。当我阅读 Java 1.6 代码时,parseInt 永远不会抛出 NPE。这是该方法的第一行:

if (s == null) {
    throw new NumberFormatException("null");
}

我敢打赌您的会话有问题。我建议使用调试器来找出设置不正确的内容。 Printf 调试也会有所帮助。

You should check the exception which should give you the line number of the NPE. In terms of your code:

HttpSession session = request.getSession(true);

Request could be null. This is highly unlikely.

String uId = session.getAttribute("userId").toString();

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 the session.setAttribute("userId", uId); was called. Or maybe your initial call to function.getLogin(username, password); returned null so you were setting null in the session?

// this is the code that [... is] throwing the NullPointerException
int userId = Integer.parseInt(uId); 

You are incorrect. As I read the Java 1.6 code, parseInt will never throw NPE. Here's the first line in that method:

if (s == null) {
    throw new NumberFormatException("null");
}

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.

つ低調成傷 2025-01-03 10:15:36

在设置属性时,您必须确保在此之前没有写入任何响应。此外,之后写入响应(也不例外)。

In setting the attribute you must ensure that upto then no response is written. Furthermore that after that the response is written (no exception).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文