Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:48:03+00:00 2026-06-17T20:48:03+00:00

I try do… Stay Logged in This is my method for add cookie… public

  • 0

I try do… Stay Logged in

This is my method for add cookie…

public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }

Call for this method is here… (another code is omitted)

@Stateless
public class CuentaUsuarioEJB implements Serializable{

    public boolean loginUsuario(CuentaUsuario cuentaUsuario, HttpSession httpSession, boolean remember, HttpServletResponse response) throws TraeloException{
            Map<String, Object> parametros = new HashMap<String, Object>();
            parametros.put("email", cuentaUsuario.getEmail());
            parametros.put("password", Encryption.encrypt(cuentaUsuario.getPassword()));
            List<CuentaUsuario> cuentasUsuarios = crudeServiceBean.findWithNamedQuery(NamedQueries.CONSULTA_LOGIN_CUENTA, parametros, 1);
            if(!Utils.isEmpty(cuentasUsuarios)){
                httpSession.setAttribute(Constantes.USER, cuentasUsuarios.get(0));

                if(remember){
                    String uuid = Encryption.encrypt(cuentasUsuarios.get(0).getEmail());
                    Utils.addCookie(response, Constantes.COOKIE_LOGIN, uuid, Constantes.COOKIE_AGE);
                }else{
                    Utils.removeCookie(response, Constantes.COOKIE_LOGIN);
                }
                return true;
            }

            return false;
        }
}

The filter…

@WebFilter(filterName="FiltroSeguridad")
public class FiltroSeguridad implements Filter{


    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpSession session = httpRequest.getSession(true);
        CuentaUsuario cuentaUsuario = (CuentaUsuario)session.getAttribute(Constantes.USER);

        //Si el usuario no ha iniciado sesión
        if(Utils.isEmpty(cuentaUsuario)){
            //Se obtiene valor de la cookie
            String uuid = Utils.getCookieValue(httpRequest, Constantes.COOKIE_LOGIN);

            if(!Utils.isEmpty(uuid)){
                Map<String, Object> parametros = new HashMap<String, Object>();
                parametros.put("email", Encryption.decrypt(uuid));

                //Existe un usuario con el valor guardado en la cookie
                List<CuentaUsuario> cuentasUsuarios = crudeServiceBean.findWithNamedQuery(NamedQueries.CONSULTA_LOGIN_CUENTA_COOKIE, parametros, 1);
                if(!Utils.isEmpty(cuentasUsuarios)){
                    session.setAttribute(Constantes.USER, cuentasUsuarios.get(0));
                    Utils.addCookie(httpResponse, Constantes.COOKIE_LOGIN, uuid, Constantes.COOKIE_AGE);
                    cuentaUsuario = cuentasUsuarios.get(0);
                }else{
                    Utils.removeCookie(httpResponse, Constantes.COOKIE_LOGIN);
                }
            }
        }

        //No existe cookie y el usuario no está logueado
        if(Utils.isEmpty(cuentaUsuario)){
            session.setAttribute(Constantes.RUTA,httpRequest.getRequestURI());
            httpResponse.sendRedirect(httpRequest.getContextPath()+"/generales/login.xhtml");
        }else{
            chain.doFilter(request, response);
        }
    }

}

And that…

public static String getCookieValue(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

The login process works fine. But I close the browser and open, in this point the method getCookieValue dont return my cookie. I dont have idea because happen that.

Thanks.

PS: Sorry for my English

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:48:04+00:00Added an answer on June 17, 2026 at 8:48 pm

    To elaborate to what @eis said, try this:

    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
            Cookie cookie = new Cookie(name, value);
            cookie.setPath( "/" );
            cookie.setMaxAge(maxAge);
            response.addCookie(cookie);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
Try this out: template <typename T> class Base { public: int someBaseMember; }; template
try { string directory = C:\\Users\\Public\\Documents; string filename = DBCS.config; string path = Path.Combine(directory,
try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception?
Try and do the following: for (var key in String.prototype) console.log(key); It gives you...nothing
Try as I might, I cannot get this to work. Here is my latest
Try to save them like this: HttpCookie latcook = new HttpCookie(latitude, lat.Value.ToString()); HttpCookie lngcook
Try this link on Firefox or Chrome: http://dl.dropbox.com/u/34375299/aaa/index.html Notice a simple animation appears on
Try to use tips for Eclipse performance I try to add option in eclipse.ini

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.