RSS

Monthly Archives: March 2015

Lecture Notes for Cookie

Unit 5 ,Chapter : 8 Cookie
One Slide per Page Download
Six Slide per Page Download

 
Leave a comment

Posted by on March 28, 2015 in Material

 

Cookie Attribute

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

public class CookieAttribute extends HttpServlet 
{
  public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
  {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//create a cookie
		Cookie c1=new Cookie("fname","mca4");
		c1.setMaxAge(60*60);
		c1.setComment("hi");
		c1.setDomain("abc.com");
		c1.setPath("/mca");
		c1.setSecure(true);
		c1.setVersion(1);
		response.addCookie(c1);
		out.println("Name = " + c1.getName() + "<br>");
		out.println("Value = " + c1.getValue()+ "<br>");
		out.println("Comment = " + c1.getComment()+ "<br>");
		out.println("MaxAge = " + c1.getMaxAge()+ "<br>");
		out.println("Domain " + c1.getDomain()+ "<br>");
		out.println("Path = " + c1.getPath()+ "<br>");
		out.println("Secure = " + c1.getSecure()+ "<br>"); //default value : false
		out.println("Version = "  + c1.getVersion()+ "<br>"); // default value : 0
		
	}
}
		 
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Modifying Cookie Values: Tracking User Access Counts

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

public class Ex3 extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		String ac =CookieUtility.getCookieValue(req,"AC","0");
		int i=Integer.parseInt(ac);
		i=i+1;
		out.println("visit counter : " + i);
		CookieUtility.createCookie(res,"AC",String.valueOf(i),3600);
	}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Using Cookies to Remember User Preferences

Ex2.java


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

public class Ex2 extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException
	{
	res.setContentType("text/html");
	PrintWriter out=res.getWriter();
	String unm=CookieUtility.getCookieValue(req,"unm","");
	String pwd=CookieUtility.getCookieValue(req,"pwd","");
	
	out.println("<html><body><form action=Ex21>"+
	"enter username <input type=text name=txtunm value="+unm+">"+
	"<br> enter password : "+
	"<input type=password name=pwd value="+pwd+"><br>"+
	"<input type=submit value=ok></form></body></html>");
	}
}

Ex21.java

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

public class Ex21 extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		
		String unm= req.getParameter("txtunm");
		String pwd= req.getParameter("pwd");
		
		CookieUtility.createCookie(res,"unm",unm,300);
		CookieUtility.createCookie(res,"pwd",pwd,300);
	}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Cookie to determine first time visitor using CookieUtility Class

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

public class Ex1 extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException ,IOException
	{
		res.setContentType("text/html");
		PrintWriter out=res.getWriter();
		
		String newu =CookieUtility.getCookieValue(req,"RV","No");
		if(newu.equals("No"))
		{
			CookieUtility.createCookie(res,"RV","Yes",300);
			out.println("Welcome to India");
		}		
		else
		{
			out.println("welcome back");
		}
	}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

CookieUtility Class

Here CookieUtility class is created to take advantage of reuseability concept.

Here in these class, we have two method:

1) To create a cookie -> createCookie() Method
2) To read particular cookie -> getCookieValue()

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

public class CookieUtility extends HttpServlet
{
		public static String getCookieValue(HttpServletRequest req,String cn,String defaultvalue)
		{
			Cookie [] cr=req.getCookies();
			if(cr != null)
			{
					for(int i=0;i<cr.length;i++)
					{
						if(cr[i].getName().equals(cn))
						{
								return cr[i].getValue();
						}
					}
			}
			return defaultvalue;
		}
		public static void createCookie(HttpServletResponse res,String cn,String cv,int age)
		{
		
			Cookie c1=new Cookie(cn,cv);
			c1.setMaxAge(age);
			res.addCookie(c1);
		}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Cookie to determine first time visitor

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

public class RepeatVisitor extends HttpServlet 
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		boolean newu=true;
		Cookie [] c=request.getCookies();
		if(c!=null)
		{
			if(c[0].getName().equals("RV"))
			{
				for (int i=0;i<c.length;i++)
				{
					if (c[i].getName().equals("RV"))
					{
						newu=false;
					}
				}
			}
		}
		
		if(newu)
		{
			Cookie c1=new Cookie("RV","Yes");
			c1.setMaxAge(60*60);
			response.addCookie(c1);
			out.println("Welcome to India");
		}
		else
		{
			out.println("welcome back");
		}
		
	}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Retrieving all cookies from the Browser

Note :This will fetch cookies only set by current domain.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class CookieDemo1 extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)
    throws ServletException,IOException
    {
         
        Cookie[] cr = req.getCookies();
        if (cr!=null)
        {
            for (int i=0;i<cr.length;i++)
            {
                out.println (cr[i].getName() + " : " + cr[i].getValue());
            }
        }
    }
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Creating a Cookie in a Servlet

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

public class CookieDemo extends HttpServlet
{
	public void doGet(HttpServletRequest req,HttpServletResponse res) 
	throws ServletException,IOException
	{
                PrintWriter out = res.getWriter();
		Cookie c1 = new Cookie("fname","mca");
		c1.setMaxAge(3600); // Set cookie age otherwise it will create session level cookie
		res.addCookie(c1);
		out.println ("cookie created successfully");
	}
}
 
Leave a comment

Posted by on March 27, 2015 in Example

 

Unit -1 Ch-10 Forms used by Website

Unit 1 Chapter : 10 (Per Page Six Slide) Download
Unit 1 Chapter : 10 (Per Page One Slide) Download

 
Leave a comment

Posted by on March 24, 2015 in Material