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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:46:37+00:00 2026-06-04T10:46:37+00:00

I am using cookies. When you first time open the page i set the

  • 0

I am using cookies. When you first time open the page i set the cookie like this

public class SessionTimeoutFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {

        Object isRegistered = session.getAttribute("logedin");
         if (isRegistered != null) {
             String isRegisteredUser = isRegistered.toString();
             if (isRegisteredUser.equalsIgnoreCase(("1"))) {
                 sessionID = UUID.randomUUID().toString();               
                 session.setMaxInactiveInterval(240);   //4min
                 Cookie userCookie = getCookie(httpServletRequest, "userCookie");
                 if (userCookie != null) {

                      //Value not setting here
                      Cookie loginUserCookie = new Cookie("userCookie", "loginUser");
                      httpServletResponse.addCookie(loginUserCookie);
                 }
                 filterChain.doFilter(httpServletRequest, httpServletResponse);
             }
         } else {
             sessionID = httpServletRequest.getRequestedSessionId();
             sessionValid = httpServletRequest.isRequestedSessionIdValid();

            //User open his browser
            if (sessionID == null && !sessionValid) {
                sessionID = UUID.randomUUID().toString();
                Cookie browserCookie = new Cookie("browserCookie", sessionID);
                httpServletResponse.addCookie(browserCookie); 
                Cookie userCookie = new Cookie("userCookie", "normal");
                httpServletResponse.addCookie(userCookie);

                session.setAttribute("logedin", "0");
                filterChain.doFilter(httpServletRequest, httpServletResponse);

            //Session expires. Each time user close the tab and session expires automatically
            } else if(sessionID != null && !sessionValid) { 
                if (httpServletRequest.isRequestedSessionIdFromCookie()) {
                    Cookie userCookie = getCookie(httpServletRequest, "userCookie");
                    String value = userCookie.getValue();

                    //Each time getting normal
                    if (value.equalsIgnoreCase("normal")) {
                        session.setAttribute("logedin", "0");
                        filterChain.doFilter(httpServletRequest, httpServletResponse);
                    } else if (value.equalsIgnoreCase("loginUser")) {

                    }
                } //end of if (httpServletRequest.isRequestedSessionIdFromCookie())
            }
         }
   } //end of dofilter()
} //end of class SessionTimeoutFilter

When first time you open the page then the condition if (sessionID == null && !sessionValid) becomes true and the userCookie set to value normal.Now if you close the browser tab, then open the page again. The cookie value is normal. OK.

But now when you log in then it comes to condition if (isRegisteredUser.equalsIgnoreCase(("1"))). Here i am trying to replace the userCookie value , like

Cookie userCookie = getCookie(httpServletRequest, "userCookie");

            if (userCookie != null) {

                String value = userCookie.getValue();

                //delete the cokie
                //userCookie.setValue("loginUser");
                //userCookie.setMaxAge(0);

                Cookie loginUserCookie = new Cookie("userCookie", "loginUser");
                httpServletResponse.addCookie(loginUserCookie);

            }

But both the approaches are not working. Now after doing that if i close the browser, and open the page again, then in the condition

if (value.equalsIgnoreCase("normal")) {

    session.setAttribute("logedin", "0");
    filterChain.doFilter(httpServletRequest, httpServletResponse);

} else if (value.equalsIgnoreCase("loginUser")) {

}

I am getting normal again as userCookie value..This time it should get me value “loginUser”, because i have changed the value of userCookie to loginUser. BUt i am getting normal here. Why i am getting the previous value. what i am doing wrong? Please tell me.

Thanks

  • 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-04T10:46:38+00:00Added an answer on June 4, 2026 at 10:46 am

    set the cookie path, solve the problem like

    Cookie userCookie = new Cookie("userCookie", "loginUser");
    userCookie.setPath("/");
    httpServletResponse.addCookie(userCookie);
    

    After setting cookie path, value is replaced.

    Thanks

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

Sidebar

Related Questions

i'm setting a cookie using this code: setcookie(Blah,user,time()+86400); i'm then checking that cookie on
I'm currently using jQuery Cookies plugin, and depending on which side of the page
can cookies be blocked using a page directive instead of doing it programmatic?
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
how can we save dragged item with cookies. I'm using Jquery with this code
I am using the jquery from https://github.com/carhartl/jquery-cookie . I am using the cookies for
I want to display a message to first time visitors of a specific page
I hope I get this question clear. I am using JavaScript cookies to save
I am creating cookie using jquery, please see below code: var divID = $(link).next(.open-block-holder).find(div:first).attr(id);
I'm no whiz at javascript or anything, actually just using cookies for the first

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.