Struts2獲取Session,獲取使用者名稱
阿新 • • 發佈:2019-02-17
// 把登入人的資訊儲存到session物件中
private String myLogin(HttpServletRequest request, String userName) {
try {
ArrayList listName = null;
HttpSession session = request.getSession();
listName = new ArrayList();
rs = selectMess(request, userName);
if (rs.next()) {
rs = selectMess(request, userName);
session.setAttribute("un", userName);//獲取使用者名稱
while (rs.next()) {
UserNameBean mess = new UserNameBean();
mess.setUserName(rs.getString("userName"));
mess.setPassword(rs.getString("password"));
listName.add(mess);
session.setAttribute("userName", listName);
}
} else {
session.setAttribute("userName", listName);
}
return "ok";
} catch (Exception e) {
e.printStackTrace();
return null;
}
private String myLogin(HttpServletRequest request, String userName) {
try {
ArrayList listName = null;
HttpSession session = request.getSession();
listName = new ArrayList();
rs = selectMess(request, userName);
if (rs.next()) {
rs = selectMess(request, userName);
session.setAttribute("un", userName);//獲取使用者名稱
while (rs.next()) {
UserNameBean mess = new UserNameBean();
mess.setUserName(rs.getString("userName"));
mess.setPassword(rs.getString("password"));
listName.add(mess);
session.setAttribute("userName", listName);
}
} else {
session.setAttribute("userName", listName);
}
return "ok";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//在Action,獲取使用者名稱
public String getUserName() {
HttpSession session = ServletActionContext.getRequest().getSession(); //獲取session,使用者名稱
return (String)session.getAttribute("un");
}