Hello,

I am new to JSP and I have to create a registration form, so when the user fills out all the information and clicks submit, the information should be stored in cookies. I cannot use database to store user information. Also after the user registers, he/she has to login and I have to validate the username and password with the user information stored in cookies. I have searched online for solution but couldnt find any!, Please Help

Register.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New User Registration</title>
</head>
<body>
    <form method="post" action="reg.jsp">
            <center>
            <table border="1" width="30%" cellpadding="5">
                <thead>
                    <tr>
                        <th colspan="2">Enter Information Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" name="uname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" name="pass" value="" /></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Submit" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>
                    </tr>
                </tbody>
            </table>
            </center>
            </form>
</body>
</html>

Dani AI

Generated

wants a registration/login flow without a database. 's JavaScript-cookie idea is a simple demo, but it leaves several important gaps that will cause problems in real use: cookies live on the client (are easily cleared or altered), have strict size limits, are not a cross-device store, and are a poor place for passwords or multi-user records.

Key constraints and safety points: most browsers constrain a single cookie to roughly 4 KB and limit the number of cookies per domain (commonly a few dozen). Cookies are readable by the client unless marked HttpOnly, so never store plaintext passwords or personally sensitive data in them. Cookies are appropriate for small pieces of non-sensitive state or for a short-lived token that is validated server-side.

A safer, practical workflow that does not require a DB:

  • Handle POSTs in a servlet (avoid putting business logic in JSP). Validate inputs server-side.
  • Hash passwords with a modern KDF (bcrypt, PBKDF2, or scrypt); do not use unsalted MD5/SHA1.
  • Persist user records to a server-side file placed under WEB-INF (CSV, JSON, or a serialized map). Use synchronized writes or file locking so concurrent registrations do not corrupt the file. Note: this is durable but not scalable.
  • For login, read the file, compare hashed passwords, then create an HttpSession attribute to represent the logged-in user (the servlet container issues JSESSIONID).
  • If a "remember me" feature is required, create a random token, persist token->username server-side, and store only the token in an HttpOnly, Secure cookie. Rotate and expire tokens.

Example (server-side cookie, for the "remember" token):

Cookie c = new Cookie("rememberToken", URLEncoder.encode(token,"UTF-8"));
c.setPath("/");
c.setMaxAge(60*60*24*30);   // 30 days
c.setHttpOnly(true);
c.setSecure(request.isSecure());
response.addCookie(c);

Troubleshooting notes: encode cookie values, keep cookie paths to "/", check request.isSecure() before sending Secure cookies, and avoid storing credentials in client-side JS. If a demo is all that is needed, limit cookies to non-sensitive fields only and make clear this is not production-safe.

you can controll cookie with javascript.
http://www.w3schools.com/js/js_cookies.asp
you can find how to use cookie here.

simple solution is here
reg.jsp

<% 
    String fname = request.getParameter("fname");
    String lname = request.getParameter("lname");
    String email = request.getParameter("email");
    String uname = request.getParameter("uname");
    String pass = request.getParameter("pass");
%>
<script>
    var cookie_name = "login_cookie_"+"<%=email%>";
    var cookie_value = "<%=fname%>" + "_" + "<%=lname%>" + ... + "<%=pass%>";//connect each value with "_"

    //get cookie
    var current_cookie = document.cookie;

    //check cookie
    if(current_cookie.indexOf(cookie_name + "=") == -1){
        //set cookie
        document.cookie = cookie_name + "=" + cookie_value;
    }else{
        //return to index page
        alert(">Already registered!!");
        location.href = "index.jsp";
    }
</script>

ps) if you use jquery, then use jquery.cookie plugin. it makes easy to use cookie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.