会话属性访问并转换为int?

发布于 2024-11-07 10:31:23 字数 514 浏览 0 评论 0原文

我已在 Servlet 中使用以下命令将用户 id 存储在会话中:

HttpSession session = request.getSession();
session.setAttribute("user", user.getId());

现在,我想从另一个 Servlet 访问该用户 id:

HttpSession session = request.getSession(false);
int userid = (int) session.getAttribute("user"); // This is not working

OR

User user = new User();
user.setId(session.getAttribute("user")); This ain't possible (Object != int)

问题:

  1. 如何转换为 int 并将 id 发送到 DAO 进行 SELECT 语句

I have stored user id in Session using following command in Servlet:

HttpSession session = request.getSession();
session.setAttribute("user", user.getId());

Now, I want to access that user id from another Servlet:

HttpSession session = request.getSession(false);
int userid = (int) session.getAttribute("user"); // This is not working

OR

User user = new User();
user.setId(session.getAttribute("user")); This ain't possible (Object != int)

Question:

  1. How can I cast to int and send the id to DAO for SELECT statement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

浊酒尽余欢 2024-11-14 10:31:23

即使您保存了 int,该方法也需要一个对象,因此您的 int 将由于自动装箱而变成 Integer。尝试将其转换回 Integer,应该没问题:

int userid = (Integer) session.getAttribute("user");

但是,如果属性为 null,您将在此处得到 NullPointerException,所以也许最好使用 Integer 一路:

Integer userid = (Integer) session.getAttribute("user");

之后,您可以安全地检查 userid 是否为 null


编辑:
为了回应您的评论,这就是我所说的“检查空值”的意思。

Integer userid = (Integer) session.getAttribute("user");
User user = null;
if (userid != null) {
    user = new UserDAO().getUser(userid);
}
// here user will be null if no userid has been stored on the session,
// and it wil be loaded from your persistence layer otherwise.

Even if you saved an int, that method expects an Object so your int will become an Integer due to auto-boxing. Try to cast it back to Integer and it should be fine:

int userid = (Integer) session.getAttribute("user");

However, if the attribute is null you will get a NullPointerException here, so maybe it's better to go with Integer all the way:

Integer userid = (Integer) session.getAttribute("user");

After this, you can safely check if userid is null.


EDIT:
In response to your comments, here's what I mean by "check for null".

Integer userid = (Integer) session.getAttribute("user");
User user = null;
if (userid != null) {
    user = new UserDAO().getUser(userid);
}
// here user will be null if no userid has been stored on the session,
// and it wil be loaded from your persistence layer otherwise.
新一帅帅 2024-11-14 10:31:23

我不擅长JAVA,但我曾经这样做过
Integer.parseInt(session.getAttribute("user").toString())

尝试一次,但一定要检查 null session.getAttribute(" user") 在调用 toString 之前

I'm not good at JAVA but I used to do it like
Integer.parseInt(session.getAttribute("user").toString())

Try once, but just be sure to check null for session.getAttribute("user") before calling toString

携余温的黄昏 2024-11-14 10:31:23

Java 有 Integer 包装类,您可以将 int 值存储在 Integer 的对象中

//setting
Integer intObj = new Integer(intVal);
session.setAttribute("key",intObj);
//fetching
Integer intObj = (Integer) session.getAttribute("key");

Java has Integer wrapper class , you can store int value in an Object of Integer

//setting
Integer intObj = new Integer(intVal);
session.setAttribute("key",intObj);
//fetching
Integer intObj = (Integer) session.getAttribute("key");
鲜血染红嫁衣 2024-11-14 10:31:23

尝试 int userid = (Integer) session.getAttribute("user");

Try int userid = (Integer) session.getAttribute("user");

停顿的约定 2024-11-14 10:31:23
Integer userid = Integer.parseInt(session.getAttribute("user"));
Integer userid = Integer.parseInt(session.getAttribute("user"));
粉红×色少女 2024-11-14 10:31:23

我用过这个:

Integer.parseInt(session.getAttribute("String").toString())

I used this:

Integer.parseInt(session.getAttribute("String").toString())
善良天后 2024-11-14 10:31:23

试试这个

int userid = Integer.parseInt(session.getAttribute("user").toString());

try this

int userid = Integer.parseInt(session.getAttribute("user").toString());
乱了心跳 2024-11-14 10:31:23

试试这个,它对我有用:
HttpSession 会话 = request.getSession();
if (session.getAttribute("user") != null) {
userid = ((Integer) session.getAttribute("用户")).intValue(); } else { 用户ID = 0; }

try this, it worked for me:
HttpSession session = request.getSession();
if (session.getAttribute("user") != null) {
userid = ((Integer) session.getAttribute("user")).intValue(); } else { userid = 0; }

温柔女人霸气范 2024-11-14 10:31:23

将会话中的两个字符串相乘:

int z = Integer.parseInt((String)session.getAttribute("sintelestis"));
int y = Integer.parseInt((String)session.getAttribute("_embadon_akinitou"));
System.out.println("Ο Συνολικός Φόρος είναι: "+ (z*y));

Multiplying two strings from session:

int z = Integer.parseInt((String)session.getAttribute("sintelestis"));
int y = Integer.parseInt((String)session.getAttribute("_embadon_akinitou"));
System.out.println("Ο Συνολικός Φόρος είναι: "+ (z*y));
空宴 2024-11-14 10:31:23

试试这个代码:

 int userId=Integer.parseInt((String)session.getAttribute("user"));

Try this code :

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