JSTL and sessions , need tip

Hello everybody.

I need some tip, regarding how I can get session variable from JSTL.

The code that I have:

<c:set var="userName" value="${sessionScope.userName}" />
<c:if test="${userName == null}" >
    <%-- redirect to login page --%>

What I am trying to do, is just before I load page check if user done login. So I try check the session variable.

The tip I need, is how I can get session variable into JSTL variable ?

Thank in advance.

That looks right, but try this as your check:

<c:set var="userName" value="${sessionScope.userName}" scope="page" />
<c:if test="${empty pageScope.userName}" >
    <%-- redirect to login page --%>

If you’re going to use the same name for variables in two different scopes, specify the scope when you’re running your check.


Thank you for helping me.
I checked it, and it seems to work now.

But i have one more thing that bothering me :frowning: And that is the reason I opened this theme.

Let say I have userLogin variable in servlet, and I just inserting it to session from the same servlet. Will the code below be right to get userLogin from session ?

<c:set var="userName" value="${sessionScope.userLogin}" />

What I am trying to ask, what exectly method to get variable stored in session to JSTL variable.

Sorry if it sounds confusing :frowning: