beginner
beginner

Reputation: 190

How to keep particular sessionScope variable(s) in use and remove the other sessionScope variable(s)?

The application allows the user to search and the result displays in the other page

For example search.xsp is for user to fill information to search and searchresult.xsp is for display the search result.

In search.xsp and searchresult.xsp, I use sessionScope variable to perform search function.

The search function is something like this:

var qstring= "";
if ((sessionScope.ItemSearch != null && sessionScope.ItemSearch != "")||(sessionScope.CategorySearch != null && sessionScope.CategorySearch != "")|| (sessionScope.LocationSearch != null && sessionScope.LocationSearch !=""))
{
    qstring = " FIELD Item contains " + sessionScope.ItemSearch +
    " | FIELD Category contains " + sessionScope.CategorySearch +
    " | FIELD Location contains " + sessionScope.LocationSearch
}
return qstring;

In the searchresult.xsp, I add a back button that will return to the search.xsp. I read the user requirement, when the user clicks the back button, it will redirect the user to the search.xsp but the previous selection should be cleared.

To clear the sessionScope variable, I found this code from this website https://openntf.org/XSnippets.nsf/snippet.xsp?id=clear-session-current-nsf

clearMap(sessionScope);
//facesContext.getExternalContext().redirect("./?logout");

// Clear Map function
function clearMap( map:Map ){ // Get iterator for the keys
    var iterator = map.keySet().iterator();  // Remove all items 
    while( iterator.hasNext() ){  
         map.remove( iterator.next() ); 
          }
}

I applied the code in search.xsp in afterPageLoad event so when I click the back button, the search.xsp will remove my previous search selection.

Here is my question.

On top of every page, there is a computed field which shows the user's login role. The reason needs to show the user's role is the user will have multiple roles in the application and I created another page to let user to choose the role that the user wants to login as.

Here is the code of that computed field

var selectrole =sessionScope.role;
if(selectrole == "Common User")
{
    return "You logged in as Common User role";
}
else if(selectrole == "Senior User")
{
    return "You logged in as Senior User role";
}
else if(selectrole == "Manager")
{
    return "You logged in as Manager role";
}
else if(selectrole == "Administrator")
{
    return "You logged in as Administrator role";
}
else
{   
}

Due to I use sessionScope variable to show user role, so if the user goes to the search.xsp and perform the search function, when the user the clicks the back button in searchresult.xsp. The clear sessionScope function will also remove sessionScope.role's value.

I searched on the internet about clear specific sessionScope variable but it seems not much information on this issue

Since the clear sessionScope variable works fine, I tried to remove specific sessionScope variable.

clearMap(sessionScope.ItemSearch);
//facesContext.getExternalContext().redirect("./?logout");

// Clear Map function
function clearMap( map:Map ){ // Get iterator for the keys
    var iterator = map.keySet().iterator();  // Remove all items 
    while( iterator.hasNext() ){  
         map.remove( iterator.next() ); 
          }
}

At the beginning, I think it is hard to remove specific sessionScope variable, and the result comes as no surprise for me that I get the error.

So is there any possible method that I can keep particular sessionScope variable(s) in use and clears the other sessionScope variable(s)?

Or any better approach for this case?

Grateful for your advice please. Thank you

Upvotes: 1

Views: 764

Answers (2)

Paul Stephen Withers
Paul Stephen Withers

Reputation: 15729

Please bear in mind what the code you link to is doing. The first line is logging the user out of HTTP completely. The rest is going through sessionScope and, for every key, removing that variable from the sessionScope map of variables.

I added that snippet to replace a simple logout button, because if a user just logs out of HTTP, then logs in as a different user, they (by default) retain the same sessionScope variables, because sessionScope is browser session, not user session.

It sounds like you don't want that snippet. Instead you just want to clear a specific sessionScope variable. sessionScope.remove("myKey") will do that, where myKey is the sessionScope variable key.

Upvotes: 2

Knut Herrmann
Knut Herrmann

Reputation: 30960

Simply set search's sessionScope variables to an empty string

sessionScope.ItemSearch = "";
sessionScope.CategorySearch = "";
sessionScope.LocationSearch = "";

This keeps the other sessionScope variables alive and your search form is cleared.

Upvotes: 1

Related Questions