简体   繁体   中英

how can i get the session attribute in desired type

from another java application, i had already set my attribute to this:

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

I'm not sure how to parse a specific attribute into a string.

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

刚投:

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

You seem to be only adding the user's name into the session attribute.

Try setting the entire User object (whatever is the domain object in your case) as follows:

session.setAttribute("SessionUser", userObj);

Then fetch the object using:

UserObj userObj = (UserObj) session.getAttribute("SessionUser");

and then use it in your query, something like this: (assuming you have a getEmail() in the UserObj)

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

if userName attribute is a String object, then you can call it with

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

or it's bean? then;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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