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 6784273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:58:13+00:00 2026-05-26T16:58:13+00:00

Update: The issue is with the setting used for the MaxAge. Setting it to

  • 0

Update: The issue is with the setting used for the MaxAge. Setting it to zero will cause the cookie to be deleted, hence it was shown in the response header but was then deleted and did not show in the request. Setting it to -1 so that it will not be stored persistently

I am doing some work on a filter in which I want it to set a cookie to indicate that the user qualifies to take a survey and that the next time he comes back to the site a survey popup window will be displayed.

I am not seeing any errors in the logs but the cookie never gets set.

Can a filter be used this way to add a cookie? Is there where a HttpServletResponseWrapper comes into play?

The thought here is that when the user comes to the the site there is a check to see if the cookie is present. If it is not then a cookie is created and added to the response. As the user navigates the site, the cookie check method is called to make sure that the hit counter is not increased for that given user.

The cookie check method never sees the cookie. Using web developer plugin for firefox, the cookie in question is not present.

Below is the filter class with the relevant methods.

public class HitCounterFilter extends TemplateFilter {
    public void doMainProcessing(ServletRequest pRequest, ServletResponse pResponse, FilterChain pChain) {
        HttpServletRequest httpRequest = (HttpServletRequest) pRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) pResponse;

        // prevent thread interference and memory consistency errors
        synchronized (lock) {
            int hitCounter = this.readFile(localFile);

            // if user has not been counted
            if (!this.checkForCookie(httpRequest, "gtgo_visitor")) {
                this.writeFile(localFile, ++hitCounter);
                this.createCookie(httpRequest, httpResponse, String.valueOf(hitCounter), "gtgo_visitor");
            }
        }
    }

    private void createCookie(HttpServletRequest pHttpRequest, HttpServletResponse pHttpResponse, String pCookieValue, String pCookieName) {
        try {
            Cookie cookie = new Cookie(pCookieName, pCookieValue);
            URL url = new URL(pHttpRequest.getRequestURL().toString());
            cookie.setDomain(url.getHost());
            cookie.setPath(this.getCookiePath(pHttpRequest));
            cookie.setComment("user is not eligible to take the survey this time");
            cookie.setMaxAge(0);

            pHttpResponse.addCookie(cookie);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    private boolean checkForCookie(HttpServletRequest pHttpRequest, String pCookieName) {
        for (Cookie cookie : pHttpRequest.getCookies()) {
            if (StringUtils.equalsIgnoreCase(cookie.getName(), pCookieName)) {
                return true;
            }
        }
        return false;
    }
} 
  • 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-05-26T16:58:13+00:00Added an answer on May 26, 2026 at 4:58 pm

    The issue is with the setting used for the MaxAge. Setting it to zero will cause the cookie to be deleted, hence it was shown in the response header but was then deleted and did not show in the request. Setting it to -1 so that it will not be stored persistently – it will be present as long as the session is active (removed once the session is closed)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE The issue is solved, all the code you can see works. Hello! I
Update: This issue was not properly explored. The real issue lies within render :json
I am going nuts here trying to resolve a cascading update/delete issue :-) I
I have this exact issue ASP.net can’t update page from event handler and it's
UPDATE: As it turns out, the below is caused by a caching issue on
can anyone help? I have an issue with linq2sql and trying to Attach (update)
I'm trying to update the display brightness from a widget but i have some
I have an issue setting a JLabel 's text using a method in the
Update Mr Wizard's answer gives pixel-perfect results, but it is Windows-only and destroys the
Update: I probably confused memory usage issues with the UI sharing same thread as

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.