cookies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gourakk
    New Member
    • Jan 2007
    • 1

    #1

    cookies

    iam trying to work with cookies. i simply wrote two statements.
    Cookie usercookie=new Cookie("user", user);
    response.addCoo kie(usercookie) ;

    will this work? or should i need to write getCookies() method also. where i need to write it and wat else i have to write?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by gourakk
    iam trying to work with cookies. i simply wrote two statements.
    Cookie usercookie=new Cookie("user", user);
    response.addCoo kie(usercookie) ;

    will this work? or should i need to write getCookies() method also. where i need to write it and wat else i have to write?

    here is an example.

    Comment

    • abctech
      New Member
      • Dec 2006
      • 157

      #3
      Originally posted by gourakk
      iam trying to work with cookies. i simply wrote two statements.
      Cookie usercookie=new Cookie("user", user);
      response.addCoo kie(usercookie) ;

      will this work? or should i need to write getCookies() method also. where i need to write it and wat else i have to write?
      Hi,
      Below is a simple demo of storing and retrieving Cookies(Session Cookies) using 2 Servlets :-

      Program 1 :- CookieDemo1.jav a
      Code:
       //Servlet CookieDemo1 is setting the Cookie 
       
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.io.*;
       
      public class CookieDemo1 extends HttpServlet
      {
       
      	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
      	{
       
      		response.setContentType("text/html");
      		PrintWriter pw = response.getWriter();
       
      		String user = "gourakk" ;
       
      // Instantiate a new Cookie object and to store info in it call the Cookie constructor, it takes 2 parameters,viz (name of cookie-String,value of cookie-String)
       
      		Cookie usercookie = new Cookie("user",user); 
      		response.addCookie(usercookie); //adding the cookie to the response, the addCookie() takes the cookie-obj as an argument and stores that cookie on the client m/c
       
      		pw.println("Cookie " + user + " is stored on your machine" ) ;
       
      	}
       
      };
       
      Program 2 :- CookieDemo2.java
      //servlet CookieDemo2 is getting the Cookie set by Servlet CookieDemo1
       
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.io.*;
       
      public class CookieDemo2 extends HttpServlet
      {
       
      	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
      	{
       
      		response.setContentType("text/html");
      		PrintWriter pw = response.getWriter();		
       
      // To get info from a cookie one can call the method getCookies() on the request object
       
      		Cookie c[] = request.getCookies();
       
      		if (c!=null)
      		{
      			for (int i=0;i<c.length;i++ )
      			{
      				String s = c[i].getName(); //getName() returns name of the Cookie
       
      				if (s.equals("user")) //'user' is the name u'd given to ur Cookie in the Demo1 servlet
      				{
      					String t = c[i].getValue(); //getValue() returns the value of that Cookie
      					pw.println("Hi the cookie value is: " + t);
      				}		
       
      			}
      		}
       
      		else
      			pw.println("No Cookies found");
      	}
       
      };
      Compile the above 2 servlets(they are 2 seperate files),deploy Demo1 first then Demo2, and check the o/p in your browser again by calling Servlet1 first and then from the same browser itself call the second Servlet
      Last edited by r035198x; Jan 9 '07, 08:34 AM. Reason: code tags

      Comment

      Working...