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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:05:41+00:00 2026-06-13T07:05:41+00:00

First time post here, I hope its a valid question. I’ve been building a

  • 0

First time post here, I hope its a valid question. I’ve been building a basic Java servlet which accepts 3 name/value pairs from a form on a page, sets those as 1. request attributes 2. session attributes and 3. cookie attributes. Cookies are then added to the response, and then the view (AccountSettings.jsp) is forwarded. The AccountSettings page is then supposed to use request.getCookies() to dump them into an array, then read the values from the array. All of this is supposed to happen every time I use this form.

My problem is that the cookie values are only correct the first time I use the form, then every time I use the form again, the cookies display the last value that was entered on page load. If I refresh the page, however, the cookies values will display correctly. I tried manually deleting the cookies in the Logout servlet (setMaxAge(0) then re-add to response) but this only produced a constant ArrayOutOfBoundsException at index 1, so I commented that portion out and leave the cookies alone.

I checked cookies associated with localhost in Chrome after the page is displayed, and the values are set correctly, so it seems to me like the JSP is drawn before the cookies are actually set correctly.

Any help on how to fix this would be appreciated. Here’s my code.

Servlet:

public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;

public Login() {

    super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    login(request, response);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    login(request, response);

}


private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

    // get a new or existing session
    HttpSession session = request.getSession();

    // Instantiate user and populate values
    User user = new User();
    user.setUser(request.getParameter("user"));
    user.setPass(request.getParameter("pass"));

    // Get last page
    String referringUrl = request.getParameter("referringPage");

    session.setAttribute("user", user.getUser());
    session.setAttribute("pass", user.getPass());
    session.setAttribute("lastPage", referringUrl);

    Cookie cookie1 = new Cookie("user", user.getUser());
    Cookie cookie2 = new Cookie("pass", user.getPass());
    Cookie cookie3 = new Cookie("lastPage", referringUrl);

    response.addCookie(cookie1);
    response.addCookie(cookie2);
    response.addCookie(cookie3);

    request.setAttribute("user", user.getUser());
    request.setAttribute("pass", user.getPass());
    request.setAttribute("lastPage", referringUrl);

    try{

        if (user.authorize()){

            session.setAttribute("name", user.getName());
            session.setAttribute("authorized", "1");

        }else{

            session.setAttribute("authorized", "0");

        }
    }
    catch(Exception e){
        e.printStackTrace();
    }

    RequestDispatcher view = request.getRequestDispatcher("AccountSettings.jsp");
    view.forward(request, response);

    user.destroy();

}

}

View:

<div id="content">
        <div class="padding">

            <%
                if (!loggedIn){
                    out.print(
                        "Oops! I'm not sure how you got here..."
                    );
                }else{
                    Cookie[] cookies = request.getCookies();

                    out.print(
                        "<h2>Account Settings</h2><br><br>" +
                        "<table>" +
                            "<tr>" +
                                "<th>Source</th>" +
                                "<th>Username</th>" +
                                "<th>Password</th>" +
                                "<th>Last Page Visted</th>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Request</th>" +
                                "<td>" + request.getAttribute("user") + "</td>" +
                                "<td>" + request.getAttribute("pass") + "</td>" +
                                "<td>" + request.getAttribute("lastPage") + "</td>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Session</th>" +
                                "<td>" + session.getAttribute("user") + "</td>" +
                                "<td>" + session.getAttribute("pass") + "</td>" +
                                "<td>" + session.getAttribute("lastPage") + "</td>" +
                            "</tr>" +
                            "<tr>" +
                                "<th>Cookies</th>" +
                                "<td>" + cookies[1].getValue() + "</td>" +
                                "<td>" + cookies[2].getValue() + "</td>" +
                                "<td>" + cookies[3].getValue() + "</td>" +
                            "</tr>" +
                        "</table>"
                    );

                }
            %>

        </div>
    </div>
  • 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-13T07:05:43+00:00Added an answer on June 13, 2026 at 7:05 am

    so it seems to me like the JSP is drawn before the cookies are actually set correctly

    That’s correct. You’re adding new cookies to the response (so they’re only available in subsequent requests on the same domain and path), but your JSP is attempting to read cookies from the current request.

    You need either to send a redirect instead of a forward by replacing

    RequestDispatcher view = request.getRequestDispatcher("AccountSettings.jsp");
    view.forward(request, response);
    

    by

    response.sendRedirect("AccountSettings.jsp");
    

    or to copy cookie values as request attributes, so that JSP can get them as request attributes (you already know how to do that).


    Unrelated to the concrete problem, passing around the password in a cookie is a very bad idea. That’s a huge security hole. For your concrete functional requirement, you’re better off storing the logged-in user as a session attribute instead.

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

Sidebar

Related Questions

This is my first time here so I hope I post this question at
first time post here. I am in the process of writing a Java program
first time post here. I was hoping someone could help me make custom SPARQL
I've been stuck on this for hours, first time post. I Don't know how
First time asking a question here. Usually I can find my answer without having
First post here, so I hope it is detailed enough. While developing an Iphone
Newbie user here, first time I post here so I'll try to make it
First time I ask something here, I hope you people will be as helpful
this is my first post here so I hope I provide all the right
this is my first time to post here. I have a problem on my

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.