0
votes

I have a java Constant.java file in which I defined session var name like this :

package com.pakageName;

public class Config {
    // name of sessions var
    public static final String ATT_SESSION_USER = "session_user";
    public static final String ATT_SESSION_MESSAGE = "session_message";
    ...
}

In Servlet file, I use the following to define session var :

session.setAttribute(Constant.ATT_SESSION_MESSAGE, "this is the content of the var I wan't to display on the page");

The Constant.java file is included in my jsp file using :

<%@ page import="com.pakageName.Config" %>

So I'm able to get the content of sessionScope var with :

// get the content of the session var
${sessionScope[Constant.ATT_SESSION_MESSAGE]}
// which return the same result as
${sessionScope.session_message}

The question is, how can I remove those session var using remove tag with Constant var value ?

I've tried the following but var attribute doesn't accept el expression ...

// throw exception because var attribute doesn't accept el
<c:remove var="${sessionScope[Constant.ATT_SESSION_MESSAGE]}" scope="session" />
<c:remove var="${Constant.ATT_SESSION_MESSAGE}" scope="session" />
// don't remove anything
<c:remove var="Constant.ATT_SESSION_MESSAGE" />
// work but the name is hard coded
<c:remove var="session_message" scope="session" />

Any idea ?

2
Shouldn't it just be: <c:remove var="Constant.ATT_SESSION_MESSAGE" scope="session" />? - Alan Hay
No, because this will try to remove the Constant var and not the session var whose name is the content of Constant.ATT_SESSION_MESSAGE (the var is called "session_message") - Thel-Rico
Sorry but you are mistaken. Remove a constant? I have no idea what you are on about. tutorialspoint.com/jsp/jstl_core_remove_tag.htm - Alan Hay
I don't want to remove a constant, the constant allow me to store the name of the session var. What I want is to remove the session var whose name is the content of the Constant var. I've edited the post to explain a little more - Thel-Rico

2 Answers

0
votes

The problem from what i can see is that you are not actually setting a session variable in the first place. All you're doing there is just calling on a public variable. So when you think you are removing a session variable, you're not actually doing anything. Which is why it persists.

Try actually setting a session variable first with:

session.setAttribute("user", Constant.ATT_SESSION_MESSAGE);

or with JSTL:

<c:set var="user" value="${Constant.ATT_SESSION_MESSAGE}" scope="session"  />

Then remove it with:

<c:remove var="user"/>
0
votes

I've find a way to achieve what I want with jsp scriptlet ...

<% session.removeAttribute(Config.ATT_SESSION_MESSAGE); %>

There is no way to obtain the same result with jstl ?