Montaser Qasem
Montaser Qasem

Reputation: 119

How to get Cookies from ServletContextListener?

i am new in Servlet and JSP and i want to grab all the cookies once my web app is running so i am using ServletContextListener to deploy what inside it once my web app is run !, so how can i get all cookies within it ? i am trying to do the following :

public class listener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
 HttpServletRequest request ;
 Cookie s[]=request.getCookies();
}

@Override
public void contextDestroyed(ServletContextEvent sce) {

}}

Upvotes: 0

Views: 397

Answers (1)

Anton Dovzhenko
Anton Dovzhenko

Reputation: 2569

Probably you don't need all the cookies [ie cookies of all users], but you need cookies of particular request. You can get them inside HttpServlet's doGet() or doPost() methods, depending on the request type:

 public class TestServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,HttpServletResponse response)
        throws ServletException, IOException {
        Cookie[] cookies = request.getCookies();
        //...
    }
 }

Upvotes: 1

Related Questions