By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

cookies

P: 1
iam trying to work with cookies. i simply wrote two statements.
Cookie usercookie=new Cookie("user", user);
response.addCookie(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?
Jan 9 '07 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
iam trying to work with cookies. i simply wrote two statements.
Cookie usercookie=new Cookie("user", user);
response.addCookie(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.
Jan 9 '07 #2

100+
P: 157
iam trying to work with cookies. i simply wrote two statements.
Cookie usercookie=new Cookie("user", user);
response.addCookie(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.java
Expand|Select|Wrap|Line Numbers
  1.  //Servlet CookieDemo1 is setting the Cookie 
  2.  
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import java.io.*;
  6.  
  7. public class CookieDemo1 extends HttpServlet
  8. {
  9.  
  10.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  11.     {
  12.  
  13.         response.setContentType("text/html");
  14.         PrintWriter pw = response.getWriter();
  15.  
  16.         String user = "gourakk" ;
  17.  
  18. // 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)
  19.  
  20.         Cookie usercookie = new Cookie("user",user); 
  21.         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
  22.  
  23.         pw.println("Cookie " + user + " is stored on your machine" ) ;
  24.  
  25.     }
  26.  
  27. };
  28.  
  29. Program 2 :- CookieDemo2.java
  30. //servlet CookieDemo2 is getting the Cookie set by Servlet CookieDemo1
  31.  
  32. import javax.servlet.*;
  33. import javax.servlet.http.*;
  34. import java.io.*;
  35.  
  36. public class CookieDemo2 extends HttpServlet
  37. {
  38.  
  39.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  40.     {
  41.  
  42.         response.setContentType("text/html");
  43.         PrintWriter pw = response.getWriter();        
  44.  
  45. // To get info from a cookie one can call the method getCookies() on the request object
  46.  
  47.         Cookie c[] = request.getCookies();
  48.  
  49.         if (c!=null)
  50.         {
  51.             for (int i=0;i<c.length;i++ )
  52.             {
  53.                 String s = c[i].getName(); //getName() returns name of the Cookie
  54.  
  55.                 if (s.equals("user")) //'user' is the name u'd given to ur Cookie in the Demo1 servlet
  56.                 {
  57.                     String t = c[i].getValue(); //getValue() returns the value of that Cookie
  58.                     pw.println("Hi the cookie value is: " + t);
  59.                 }        
  60.  
  61.             }
  62.         }
  63.  
  64.         else
  65.             pw.println("No Cookies found");
  66.     }
  67.  
  68. };
  69.  
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
Jan 9 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.