简体   繁体   English

如何从不同的servlet获取会话属性数据成员?

[英]how to get session attribute data members from different servlets?

I have set session attribute like this: 我已经设置了会话属性,如下所示:

request.getSession().setAttribute("newEmployee", retEmp);

now this attribue is an abject of type Employee that has data member "id", I want to get this from another servlet, so im trying to do something like this: 现在,此属性是具有数据成员“ id”的Employee类型的对象,我想从另一个servlet中获取它,所以我试图做这样的事情:

request.getSession().getAttribute("newEmployee.id").toString()

isn't it suppose to work? 它不是应该工作吗? tnx n

You need to retrieve the value by the same key you put it in under. 您需要使用与输入相同的键来检索值。 Then you need to cast it to the sort of object you stored on the session. 然后,您需要将其强制转换为会话中存储的那种对象。 Once you do, you can manipulate it as you please. 完成后,您可以随意对其进行操作。

Employee e = (Employee) request.getSession().getAttribute("newEmployee");
String id = e.id;

That's the way we do thing in a strongly typed language like Java (excecpt for the hashing part which would work the same most anywhere). 这就是我们使用Java之类的强类型语言处理事情的方式(哈希部分的摘录部分,该部分在大多数地方都可以使用)。

你只需要

String empId = ((Employee) request.getSession().getAttribute("newEmployee")).getId();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM