Site icon DataFlair

Session Management using Cookies in JSP

Program 1

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
    </body>
</html>

Program 2

<%-- 
    Document   : StudentInfo
    Created on : Dec 2, 2023, 1:50:42 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
     <center>
         <form method="post" action="TestCookie1.jsp">
             <table border="1">
                 <tr>
                     <th>Student Name</th>
                     <td><input type="text" name="txtname"></td>
                 </tr>                     
                 <tr>
                     <th>Password</th>
                     <td><input type="password" name="txtpass"></td>
                 </tr>                     
                 <tr>
                     <th>Course</th>
                     <td><input type="text" name="txtcourse"></td>
                 </tr>                     
                 <tr>
                     <th>Email Id</th>
                     <td><input type="text" name="txtemail"></td>
                 </tr>    
                 <tr>
                     <td></td>
                     <td><input type="submit" value="Submit"></td>
                 </tr> 
             </table>    
         </form>
     </center>         
    </body>
</html>

Program 3

<%-- 
    Document   : TestCookie1
    Created on : Dec 2, 2023, 1:55:31 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page  import="javax.servlet.http.Cookie"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World This is Page 1!</h1>
        <center>
        <% 
             String sname,spass,scourse,semail;
             sname=request.getParameter("txtname");
             spass=request.getParameter("txtpass");
             scourse=request.getParameter("txtcourse");
             semail=request.getParameter("txtemail");
             
             out.println("Name:" +sname);
             out.println("Password:" +spass);
             out.println("Course:" +scourse);
             out.println("Email:" +semail);
             Cookie C1=new Cookie("name", sname);
             Cookie C2=new Cookie("password", spass);
             Cookie C3=new Cookie("course", scourse);
             Cookie C4=new Cookie("email", semail);
             response.addCookie(C1);
             response.addCookie(C2);
             response.addCookie(C3);
             response.addCookie(C4);
             
        %>
        <form method="post" action="TestCookie2.jsp">
            <input type="text" name="txtmobile">
            <input type="submit" value="Submit">
        </form>    
        </center>
    </body>
</html>

Program 4

<%-- 
    Document   : TestCookie2
    Created on : Dec 2, 2023, 2:03:04 PM
    Author     : admin
--%>
<%@page  import="javax.servlet.http.Cookie" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World! This is Page 2</h1>
        <% 
            Cookie mycookie[];
            mycookie=request.getCookies();
            for(int i=1;i<mycookie.length;i++)
            {     
               out.println("<br>Cookie Name: "+mycookie[i].getName());
               out.println("<br>Cookie Value :"+mycookie[i].getValue());
            } 
          String smobile;
          smobile=request.getParameter("txtmobile");
          out.println("<br>Mobile No:" +smobile);
          Cookie C5=new Cookie("mobile",smobile);
          response.addCookie(C5);
        %>
         <form method="post" action="TestCookie3.jsp">
            <input type="submit" value="Submit">
        </form>    
    </body>
</html>

program 5

<%-- 
    Document   : TestCookie3
    Created on : Dec 2, 2023, 2:19:04 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        
        <% 
            Cookie mycookie[];
            mycookie=request.getCookies();
            for(int i=1;i<mycookie.length;i++)
            {     
               out.println("<br>Cookie Name: "+mycookie[i].getName());
               out.println("<br>Cookie Value :"+mycookie[i].getValue());
            } 
          
        %>
    </body>
</html>
Exit mobile version