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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:19:03+00:00 2026-06-05T21:19:03+00:00

i have written the next code: public void delete(MyType instance) { List<MyType> myList =

  • 0

i have written the next code:

public void delete(MyType instance) {
        List<MyType> myList = this.getAll();

        Cookie[] cookies = request.getCookies();
        List<Cookie> cookieList = new ArrayList<Cookie>();
        cookieList = Arrays.asList(cookies);
        for(Cookie cookie:cookieList) {
            if(Long.valueOf(cookie.getValue()) == instance.getId()) {
                cookieList.remove(cookie);
            }
        }
        myList.remove(instance);
        cookies = (Cookie[]) cookieList.toArray();
}

the issue is next: when i delete the cookie from the cookielist, how i can put the updated cookielist (without deleted cookie) back to the client? request or response don’t have any *.setCookies(); methods.
or cookies will update automatically?
best regards.

  • 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-06-05T21:19:04+00:00Added an answer on June 5, 2026 at 9:19 pm

    You need to set the very same cookie with a null value and a max age of 0 (and the same path, if you have set a custom one) back on the response by HttpServletResponse#addCookie().

    cookie.setValue(null);
    cookie.setMaxAge(0);
    cookie.setPath(theSamePathAsYouUsedBeforeIfAny);
    response.addCookie(cookie);
    

    Unrelated to the concrete problem, you do not need massage the array to a list and back at all. The enhanced for loop works on arrays as good. Also, using == to compare Long values would only work for values between -128 and 127. You need equals() instead. So all in all, the method could look like this:

    public void delete(MyType instance) {
        Cookie[] cookies = request.getCookies();
    
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (Long.valueOf(cookie.getValue()).equals(instance.getId())) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);
                    cookie.setPath(theSamePathAsYouUsedBeforeIfAny);
                    response.addCookie(cookie);
                }
            }
        }
    }
    

    By the way, it’s scary to see request and response being instance variables of some class. Are you sure that the particular class is threadsafe? To understand servlets and threadsafety, you may find this answer helpful: How do servlets work? Instantiation, sessions, shared variables and multithreading.

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

Sidebar

Related Questions

I have written this code: public void reloadDefConfig(File filepath, String fileInJar) { File configFile
i have the next code written in C#....it create a tex file using utf-8.....the
I have written this piece of code here and I have linked it alright
I have written such the code below that ; I have an array list
I have written a program that uses qhttp to get a webpage. This works
I have sample code written for parsing xml file using javax.xml package. it uses
I have written a code for downloading a file(blob in database table).I get a
I have written a code for a login page and user page. When the
I have written a greasemonkey script which will do GM_xmlhttpRequest to the next 2
I have written my own 2-3-4 tree in java. Currently, my code looks like

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.