If your application is web, this information is usually saved in the application section. Here’s a good explanation of sessions:
http://klauslaube.com.br/2012/04/05/entendendo-os-cookies-e-sessoes.html
If your application is desktop, you can save it in a Map that is publicly accessed, as a English and synchronized. See below for the definition of Singleton:
http://en.wikipedia.org/wiki/Singleton_pattern
Session
See below for an example using HTTP Session for Servlets:
....
import javax.servlet.http.HttpSession;
....
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
....
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// recupera a sessõa
HttpSession session = request.getSession();
// colocar um valor na sessão
session.setAttribute("user", "Pankaj");
....
// recupera valor da sessão
String userName = (String) session.getAttribute("user");
....
}
....
}
Desktop
See a class that could be used for a Desktop session:
package com.myapp.session;
import java.util.Hashtable;
import java.util.Map;
public class Session {
// note that HashTable is synchronized
private Map<String, Object> _map = new Hashtable<String, Object>();
private static Session _session = new Session();
public static Session getSession() {
return _session;
}
public void setAttribute(String key, Object value) {
_map.put(key, value);
}
public Object getAttribute(String key) {
return _map.get(key);
}
}
Hence, to use the session do the following:
Session session = Session.getSession();
....
// coloca um valor na sessão
session.setAttribute("user", "Pankaj");
....
// recupera valor da sessão
String userName = (String) session.getAttribute("user");
When the user logs in you can play it in the session
HttpSession sessao = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
, if your application is web. read here httpsession– Wellington Avelino