Tuesday, October 26, 2010

JSP Session Mgmt-Using Cookie


JSP-Session Management using Cookies


Introduction:
                        Cookies are small information that is send to the server from browser for later use. When we manage session in jsp, cookies are used to manage session. There are four ways to manage the session. In this lesson, we will learn how to create cookies and how to use cookies in jsp. Cookie is encapsulated in javax.servlet.http.Cookie.

Create cookies:

                       
                        Cookie c=new Cookie(“CookieName”,”CookieValue”);
                        response.addCookie(c);

Retrieve cookies:

                       
                        Cookie[ ] display=request.getCookies();


Kill cookies:


Problem:

            There are three files. 1). cookie.jsp  2). cookie2.jsp 3). cookie3.jsp

cookie.jsp have one text box to get firstname and click OK the control goes to cookie2.jsp. cookie2.jsp have one text box to get lastname and click OK the control goes to cookie3.jsp. where we will display first name and last name .


Screen shot:

cookie.jsp

cookie2.jsp

cookie3.jsp
























Procedure:

Step 1:

            Create Project named as CookiesPro



Step 2:

            Create Folder named as jsp























Step 3:

3.1). Create jsp file named as cookie.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< br > < br >
< form action="http://localhost:8084/CookiesPro/jsp/cookie2.jsp" method="post" >
First Name: < input type="text" name="fname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

3.2). Create another jsp file named as cookie2.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b > < br >
< %

String fname=request.getParameter("fname");

%>
< %

Cookie c=new Cookie("firstname",fname);
response.addCookie(c);

% >
< form action="http://localhost:8084/CookiesPro/jsp/cookie3.jsp" method="post" >
Last Name < input type="text" name="lname"/ > < br >
< input type="submit" value="OK"/ >
< /form >
< /body >
< html >

3.3). Create another jsp file named as cookie3.jsp



< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP-Cookie Example < /title >
< /head >
< body >
< b > Cookie Example < /b >
< %

String lname=request.getParameter("lname");

% >

< %
Cookie c1=new Cookie("lastname", lname);
response.addCookie(c1);

% >


< form action="http://localhost:8084/CookiesPro/jsp/display.jsp" method="post" >
< input type="submit" value="OK"/ >
< /form >
< /body >
< /html >

3.4). Create another jsp file named as display.jsp

< %@page contentType="text/html" pageEncoding="UTF-8"% >
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
http://www.w3.org/TR/html4/loose.dtd >

< html >
< head >
< title > JSP-Display all Cookie < /title >
< /head >
< body >
< %
Cookie[] dis=request.getCookies();
for(int i=0;i
{
out.println(dis[i].getName()+":"+dis[i].getValue());
out.println("");
% >
< br >
< %
}
% >
< /body >
< /html >




Step 4:

            Run the cookie.jsp

            Suresh as Firstname

            Kumar as Lastname

Step 5:

            Get output


Structure of CookiePro


No comments:

Post a Comment