java servlet cookie

      No Comments on 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

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\

<%
	response.sendRedirect("./LastVisitChkUsingCookies");
%>

 

File Name : LastVisitChkUsingCookies.java

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

/*
		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) 
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class LastVisitChkUsingCookies extends HttpServlet
{

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
	{
		PrintWriter out = response.getWriter();
		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();
		String last_visit_date=null;
		
		if (cookies != null) 
		{
			for(int i=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 = new Date();
			Cookie c = new Cookie("last_visit",now+"");
			c.setMaxAge(60*60*24*365);  //1 year
			response.addCookie(c);
		}
		else
		{
			Date now = new Date();
			
			Cookie c = new Cookie("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\

<?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 Practical 24  </display-name>
  <description> 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) </description>
  
  <servlet>
	<servlet-name>LastVisitChkUsingCookies</servlet-name>
	<servlet-class>LastVisitChkUsingCookies</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>LastVisitChkUsingCookies</servlet-name>
	<url-pattern>/LastVisitChkUsingCookies</url-pattern>
  </servlet-mapping>
    
</web-app>

 

Output (First Time)

GTU WTAD Practical 24

Output (Second Time Visit)

GTU WTAD Practical 24

Leave a Reply