Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 9: Servlet Cookie-Based Login (4 Users)

Lesson 10 of 13 in the free Web Technology Lab (BCS552) notes on Siksha Sarovar, written by Rohit Jangra.

Program Statement

Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2, pwd3 and pwd4 respectively. Write a servlet to: (1) create a Cookie and add these four user IDs and passwords to it, and (2) read the user ID and password entered in the login form and authenticate against the values available in the cookie.

The Real Servlet Code (deploy on Tomcat)

// SetCookieServlet.java — runs once to create the auth cookie
@WebServlet("/set-cookie")
public class SetCookieServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String data = "user1:pwd1,user2:pwd2,user3:pwd3,user4:pwd4";
        Cookie authCookie = new Cookie("authData", data);
        authCookie.setMaxAge(60 * 60); // 1 hour
        resp.addCookie(authCookie);
        resp.getWriter().println("Auth cookie created!");
    }
}

// LoginServlet.java — reads the login form and checks it against the cookie
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        String uid = req.getParameter("uid");
        String pwd = req.getParameter("pwd");
        boolean authenticated = false;

        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if ("authData".equals(c.getName())) {
                    for (String pair : c.getValue().split(",")) {
                        String[] parts = pair.split(":");
                        if (parts[0].equals(uid) && parts[1].equals(pwd)) {
                            authenticated = true;
                        }
                    }
                }
            }
        }
        resp.getWriter().println(authenticated ? "Login successful!" : "Invalid credentials.");
    }
}

Browser Simulation

document.cookie is the browser's equivalent of the Servlet Cookie API. The demo below stores the four user/password pairs in a cookie on page load, then a login form reads and checks document.cookie — exactly mirroring the servlet logic above, fully client-side.

CO Mapping

CO5