JSTL gets an object from a session

I put the object into the session:

session.setAttribute("userDTO", currentUser);

And I'm trying to display it using EL. I succeeded with scriplets (proving that the object is set perfectly in the session).

Code in JSP:

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

The scripts display the username, but after "Username from the session" nothing is displayed. 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;
    }
}
+4
source share
2 answers

Step 1: Put jstl-x.x.jarin the lib folder or install the dependency in maven.

Step 2: In the jspfile

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 ...
 <body>
     <% UserDTO userdto=(UserDTO)session.getAttribute("userDTO"); %>
     <%=userdto.getUsername() %>
     Username from session:<c:out value="${sessionScope.userDTO.username }"/>
      ...
 </body>
0
source

In your JSP, you can simply do this using an expression (its named EL expression language) -

<body>
  Username from session : ${sessionScope.currentUser}
</body>
+3
source

Source: https://habr.com/ru/post/1544609/


All Articles