Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSTL get object from session

I have put an object in the session:

session.setAttribute("userDTO", currentUser);

And I am trying to display it using EL. I have succeeded with scriplets (proving that the object is set in the session fine).

Code in JSP:

<body>
    <% UserDTO userdto=(UserDTO)session.getAttribute("userDTO"); %>
    <%=userdto.getUsername() %>
    Username from session:<c:out value="${sessionScope.userDTO.username }"/>
</body>

The scriplets display the username but nothing is displayed after "Username from session:". Why?

UserDTO class:

public class UserDTO {
    private int ID;
    private String email;
    private boolean emailConfirmed;
    private String username;
    private String role;
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public boolean isEmailConfirmed() {
        return emailConfirmed;
    }
    public void setEmailConfirmed(boolean emailConfirmed) {
        this.emailConfirmed = emailConfirmed;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
}
like image 209
VladTh Avatar asked Oct 21 '22 06:10

VladTh


1 Answers

In your JSP you can simply do it by using the expression (its called EL - expression language) -

<body>
  Username from session : ${sessionScope.currentUser}
</body>
like image 86
Maverick Avatar answered Jan 02 '23 21:01

Maverick