• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need variable to maintain value across multiple involkations of same procedure  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a little hard to explain but the concept is simple.

I have a java application which when the user clicks on the SUBMIT button, I want to display a pop-up message one time. It could happen that there are user errors on the screen being submitted and if so I want to let the application do its' normal process to force the user to fix the user errors (after the pop-up message is displayed once).

I can get the pop-up to display just as I want. The problem is that the popup displays a second time if there are user errors. If when the pop-up displays, I could set a variable (such as alreadyDisplayed), then I could make the program avoid displaying the pop-up a second time.

What I don't know how to do is how to create such a variable (note that my terminology may be wrong). I want the variable to be session dependent (meaning that each user gets their own copy of alreadyDisplayed).


Any thoughts on how to implement?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a web app or a Swing app?

If a web app, then the already-mentioned session is a good place to place such a variable.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
web
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking for the Java equivalent of $_SESSION['CompanyName']; in PHP

I don't know how to "say" this in Java
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the EL Reference. If you are using JSP or JSF, you would do something like:

the context can be the request, session, or application -- I believe they will be searched in that order.
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot page scope, which comes even before request scope.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the answer that I needed was:

FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getSessionMap().put("TOC", "Y");
 
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Piaser wrote:the answer that I needed was:

FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getSessionMap().put("TOC", "Y");


No it wasn't.

What you want, formally stated, is to preserve state in a user session from one JSF request to another.

Which is about as common a task as one can come up with in a JSF web application.

However, you've violated JSF Rule #1 by using brute force to create/access your session object. JSF Rule #1 states that the more JSF-specific your code is, the more likely that you're doing it wrong.

A much cleaner way would have been to make the "TOC" object be a JSF Managed Object and instruct JSF to inject it into the code. That way you don't have to muck about with the FacesContext at all.

We do have a forum just for the JavaServer Faces platform here on the JavaRanch. I encourage you to use it when you have JSF-related questions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!