简体   繁体   English

启动页面时删除Cookies

[英]Delete Cookies on Page start up

I'm trying to delete a cookie on start up of a Liferay Server webpage. 我正在尝试在Liferay Server网页启动时删除cookie。 The page is completely static and its content is build up with wigdets. 该页面是完全静态的,其内容由wigdets组成。 I'm new to this stuff, so I received a hint to create a new wigded, which is loaded on that page and its only function is to delete some specific cookies. 我是新手,因此收到了创建新的wigded的提示,该提示已加载到该页面上,其唯一功能是删除某些特定的cookie。

So i put a new wigded on this page, made him load some jsp file, and this file contains following code: 因此,我在此页面上添加了新的wigded,让他加载了一些jsp文件,并且该文件包含以下代码:

<%
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
    for (Cookie cookie:cookies)
    {
        if(cookie.getName().equals("Cookie1")  || cookie.getName().equals("Cookie2"))
        {
            Cookie clearCookie = new Cookie(cookie.getName(), null);
            clearCookie.setMaxAge(0);
            clearCookie.setPath("/");
            response.addCookie(clearCookie);
        }
    }
}
%>

So far this one works partially. 到目前为止,这一部分工作。 It receives all Cookies from the request Object, it also finds both cookies I'm looking for, but when it comes to that part where the cookies has to be replaced, there is no change. 它从请求对象接收所有cookie,它也找到我要查找的两个cookie,但是当涉及必须替换cookie的那一部分时,它没有任何变化。 Even if i try to create new cookies, it doesn't work I suppose it has to do with the response object, because I probably have to deliver it to something. 即使我尝试创建新的cookie,它也不能与响应对象有关,因为我可能必须将其传递给某些对象。 But how can I achieve this? 但是我该如何实现呢?

I also tried it with Javascript like this: 我也用这样的Javascript尝试过:

<script>
    window.onload = function() {
      document.cookie = "Cookie1=;Path=/";
      document.cookie = "Cookie2=;Path=/";
    };
</script>

Here is the problem, that the cookies I wanted to replace, weren't replaced. 这是我要替换的cookie没有被替换的问题。 Instead this script just created a second batch of cookies with the same Name and Path like the first one but with an empty value. 相反,此脚本仅创建了第二批cookie,它们的名称和路径与第一批相同,但值为空。

Here is how we managed to control cookies. 这是我们设法控制Cookie的方法。 You can create hook which extends Action class. 您可以创建扩展Action类的钩子。 Here are parts of code, you can use. 这是部分代码,您可以使用。

public class ManageCookiesAction extends Action {

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response)
        throws ActionException {

    Cookie[] cookies = request.getCookies();

    /** Manage cookies in you scenario... **/

    }
}

then in ../resource/portal.properties 然后在../resource/portal.properties中

servlet.service.events.pre=net.your.hook.class.package.ManageCookiesAction

We were using it to manage redirection to site saved in cookies of each user (they could have their own home sites on our portal) Might be useful in your scenario as well. 我们使用它来管理重定向到保存在每个用户的Cookie中的网站(他们可能在我们的门户网站上拥有自己的主页),在您的情况下也可能有用。 Good luck. 祝好运。

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

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