logout error servlet cookie

1

Hello. I'm having an error on the line:

Cookie cookie = new Cookies(req.getCookies().getUsuarioLogado();

The complete method / class is this:

@WebServlet(urlPatterns = "/logout")
public class Logout extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    //String getCookies = req.getParameter(getCookies);
    //String getUsuarioLogado = req.getParameter(getUsuarioLogado);

    Cookie cookie = new Cookies(req.getCookies().getUsuarioLogado();
    if(cookie!=null) {
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
    }
    PrintWriter writer = resp.getWriter();
    writer.println("<html><body>Logout efetuado</body></html>");
    }
}

I can not solve the error. Anyone know?

Thank you

    
asked by anonymous 12.07.2016 / 14:49

2 answers

0

I managed to fix it, my current code looks like this:

public class Logout extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    //String getCookies = req.getParameter(getCookies);
    //String getUsuarioLogado = req.getParameter(getUsuarioLogado);

    Cookie cookie = new Cookies(req.getCookies()).getUsuarioLogado();
    if(cookie!=null) {
        cookie.setMaxAge(0);
        resp.addCookie(cookie);
    }
    PrintWriter writer = resp.getWriter();
    writer.println("<html><body>Logout efetuado</body></html>");
}

}

    
12.07.2016 / 21:01
2

Hi! For you to generate a cookie is as follows:

Cookie cookie = new Cookie("nome_cookie", "valor_cookie");

The getCookies method returns the cookie list. It does not have the getUsuarioLogado method.

To retrieve the cookie you want, you must iterate through the list returned by getCookies.

example:

Cookie[] cookies = req.getCookies();
for(Cookie cookie : cookies) {
  if(cookie.getName().equals("nome_que_você_deseja") {
     // faz o que você precisa
    break;
  }
}

I hope I have helped.

    
12.07.2016 / 15:38