我想在登錄後顯示登錄的用戶全名,但需要一些direction.I已經驗證用戶對數據庫,但它只能從login.How返回電子郵件有關登錄用戶的信息。我有三個類LoginDAO,Session和managedbean,loginDAO包含從數據庫中獲取所需信息的查詢。在java中登錄後返回全名
public class Users {
private int userid;
private String fname;
private String lname;
private String email;
private String password;
private String profileimg;
Profile uprofile = new Profile();
public Profile getUprofile() {
return uprofile;
}
public void setUprofile(Profile uprofile) {
this.uprofile = uprofile;
}
public String getProfileimg() {
return profileimg;
}
public void setProfileimg(String profileimg) {
this.profileimg = profileimg;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class LoginDAO {
public static Users validate (String user,String password) throws SQLException{
Connection con = null;
PreparedStatement ps = null;
Users validUser = null;
try{
con = DataConnect.getConnection();
ps = con.prepareStatement("Select fname,lname,email, password from users where email = ? and password = MD5(?)");
ps.setString(1, user);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
validUser = new Users();
validUser.setFname(rs.getString("fname"));
validUser.setLname(rs.getString("lname"));
return true;
return validUser;
}
}catch (SQLException ex){
System.out.println("Login error -->" +
ex.getMessage());
return null;
}finally{
DataConnect.close(con);
}
return validUser;
}}
這是管理bean @ManagedBean(name = 「loginbean」) @SessionScoped 公共類LoginBean實現Serializable { 私人字符串FNAME; private String pwd; private String msg; 私人字符串用戶;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
此方法調用loginDAO並將其與用戶輸入相匹配並啓動會話。
public String validateUsernamePassword() throws SQLException {
Users validUser = LoginDAO.validate(user, pwd);
if (validUser != null) {
HttpSession session = SessionUtils.getSession();
session.setAttribute("username", validUser.getEmail());
session.setAttribute("firstname", validUser.getFname());
return "feed";
} else {
return "login";
}
}
//logout event, invalidate session
public String logout() {
HttpSession session = SessionUtils.getSession();
session.invalidate();
return "login";
}
<h:form>
<p>Welcome <h:outputText id="username" value="#
{loginbean.user}"/></p>
<h:outputText id="firstname" value="#{loginbean.fname}"/>
<h:commandLink action="#{loginbean.logout}" value="Logout">
</h:commandLink>
</h:form>
請修復您的代碼格式化。 –
和你的程序有什麼問題? – whyn0t