java servlet cookie

java servlet cookie

GTU WTAD Practical 24 : Develop an application to demonstrate how the client (browser) can remember the last time it visited a page and displays the duration of time since its last visit. (Hint: use Cookie)

java servlet cookie example I have explained, how to use Cookies in Java Servlet. Here I have stored last visited date and time on Cookies sothat next time I can check weather user is coming first time or have visited before.

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu24\

1

2

3

<%

response.sendRedirect("./LastVisitChkUsingCookies");

%>

File Name : LastVisitChkUsingCookies.java

File Location : Tomcat_Apache\webapps\gtu24\WEB-INF\classes

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

/*

GTU WTAD Practical 24 : Develop an application to demonstrate how the client (browser) can remember the last time it visited a page and displays the duration of time since its last visit. (Hint: use Cookie)

out.println("<html><head><title>GTU WTAD Practical 24 : Develop an application to demonstrate how the client (browser) can remember the last time it visited a page and displays the duration of time since its last visit. (Hint: use Cookie) </title></head><body><h2>GTU WTAD Practical 24 : Develop an application to demonstrate how the client (browser) can remember the last time it visited a page and displays the duration of time since its last visit. (Hint: use Cookie) </h2>");

Cookie[]cookies=request.getCookies();

Stringlast_visit_date=null;

if(cookies!=null)

{

for(inti=0;i<cookies.length;i++)

{

Cookie cookie=cookies[i];

if(cookie.getName().equals("last_visit"))

{

last_visit_date=cookie.getValue();

break;

}

}

}

if(last_visit_date!=null)

{

out.println("Your last visit was on : "+last_visit_date);

Date now=newDate();

Cookiec=newCookie("last_visit",now+"");

c.setMaxAge(60*60*24*365);//1 year

response.addCookie(c);

}

else

{

Date now=newDate();

Cookiec=newCookie("last_visit",now+"");

c.setMaxAge(60*60*24*365);//1 year

response.addCookie(c);

out.println("Welcome to Website... Hope You will Visit Again");

}

out.println("</body></html>");

}

}

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu24\WEB-INF\

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<?xml version="1.0"encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0"

metadata-complete="true">

<display-name>GTU WTAD Practical24</display-name>

<description>Develop an application todemonstrate how the client(browser)can remember the last time it visitedapage anddisplays the duration of time since its last visit.(Hint:useCookie)</description>