Capture information and use when you need Java

Asked

Viewed 710 times

0

I am thinking of implementing a function that captures the logged in user, I still can not imagine how I can implement such a function, I would like to know what is usual and propitious for me to register the name and the user code while the software runs. My application is made in Java.

  • 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

1 answer

2


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");
  • Okay, Eduardo has some example I can imagine to start figuring out how I’m going to do it? Thank you

  • Is your application web or desktop? If it’s web, what technology are you using?

  • my application is Desktop

  • 2

    See now that you have an example of how to use sessions for Sérvlets and how to create and use sessions for Desktop applications.

  • If the answer is correct, please "tick" the green arrow so abiaxo of the question score. Thank you :)

  • Eduardo I just tested here and it worked thank you very much for the help hugs.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.