繁体   English   中英

如何从ServletContextListener获取Cookies?

[英]How to get Cookies from ServletContextListener?

我是Servlet和JSP的新手,我想在我的Web应用程序运行后立即获取所有cookie,因此一旦我的Web应用程序运行,我将使用ServletContextListener在其中部署所有内容!因此,如何在其中包含所有cookie? 我正在尝试执行以下操作:

public class listener implements ServletContextListener {

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

@Override
public void contextDestroyed(ServletContextEvent sce) {

}}

可能您不需要所有cookie(即所有用户的cookie),但是您需要特定请求的cookie。 您可以根据请求类型在HttpServlet的doGet()或doPost()方法中获取它们:

 public class TestServlet extends HttpServlet {

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM