Followers

JSP: Session tracking using Cookies

index.html
==========
<form action="first.jsp" >
Name:<input type="text" name="userName"/><br/>
Address :<input type="text" name="address"/><br/>

<input type="submit" value="submit"/>
</form>

first.jsp
=========

<%@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>
        <%
    String name=request.getParameter("userName");
    String addr=request.getParameter("address");
    out.print("Hello "+name);
    out.println("<br /><br />");
    out.print("Address "+addr);
    out.println("<br /><br />");
   
    Cookie ck1=new Cookie("uname",name);//creating first cookie object
    response.addCookie(ck1);//adding first cookie in the response
    Cookie ck2=new Cookie("uaddr",addr);//creating second cookie object
    response.addCookie(ck2);//adding second cookie in the response

    //creating submit button
    out.print("<form action='second.jsp'>");
    out.print("<input type='submit' value='submit'>");
    out.print("</form>");
         
    out.close();
    %>
    </body>
</html>

second.jsp
===========

<%@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>
        <%      
    Cookie ck[]=request.getCookies();
    out.print("Hello "+ck[0].getValue());
    out.println("<br /><br />");
    out.print(" Address is "+ck[1].getValue());

    out.close();
       
        %>
    </body>
</html>

No comments:

Post a Comment