如何获得所需类型的会话属性

时间:2011-11-07 14:54:21

标签: java servlets

来自另一个java应用程序,我已经将我的属性设置为:

     HttpSession session = request.getSession(true);
     session.setAttribute("SessionUser", userName);

我不确定如何将特定属性解析为字符串。

String query = "SELECT title, first, last, email FROM member WHERE email="+**??**;

3 个答案:

答案 0 :(得分:2)

刚演员:

String username = (String) session.getAttribute("SessionUser");

答案 1 :(得分:1)

您似乎只是将用户的名称添加到会话属性中。

尝试设置整个User对象(在您的情况下是域对象),如下所示:

session.setAttribute("SessionUser", userObj);

然后使用:

获取对象
UserObj userObj = (UserObj) session.getAttribute("SessionUser");

然后在您的查询中使用它,如下所示: (假设您在UserObj中有一个getEmail())

String userEmail = userObj.getEmail();
String query = "SELECT title, first, last, email FROM member WHERE email="+ userEmail;

答案 2 :(得分:0)

如果userName属性是String对象,则可以使用

调用它
String userName= (String)session.getAttribute("SessionUser");
String query = "SELECT title, first, last, email FROM member WHERE email="+ userName;

还是它的豆子?然后;

UserBean user = (UserBean)session.getAttribute("SessionUser");
String query = "SELECT title, first, last, email FROM member WHERE email="+ user.getUserName();