Start session with java

1

I'm doing a web page in jsp with netbeans and I need to validate that the user has logged in to show some pages that should be restricted I've done this before but with php in the following way

session_start();
if (!isset($_SESSION['missael']))
    header('Location: index.php');

Now I need to implement it with java, any ideas?

    
asked by Missael Armenta 30.05.2016 в 18:13
source

1 answer

1

In Java EE, the session is handled through the HttpSession interface % You can get the session using the HttpServletRequest#getSession method :

HttpServletRequest request = ...; //quizás sea parámetro de tu método
//obtienes la sesión
HttpSession session = request.getSession();
//puedes colocar datos en la sesión
session.setAttribute("usuario", "algo");
//puedes obtener dicho dato luego
//como devuelve un Object, debes hacer el casteo apropiado
String algo = (String)session.getAttribute("usuario");
//puedes remover datos de la sesión
session.removeAttribute("remueveme");
//para cerrar la sesión (en el logout), utilizas el método invalidate
session.invalidate();
    
answered by 30.05.2016 / 18:35
source