473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

cookies

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
2 1175
r035198x
13,262 8TB
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
abctech
157 100+
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

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

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
20
by: Brian Burgess | last post by:
Hi all, Anyone know if this is possible? If so, on which page would the cookie be? .. On the page calling a function defined in the include file? thanks in advance.. -BB
9
by: | last post by:
Is it possible for a user to enable permanent cookies but disable session cookies.....this seems like a contradition yet this is what I appear to be reading in online articles?
1
by: John Taylor-Johnston | last post by:
I'm a University academic looking for a proper definition of JavaScript Cookies. http://www.CollegeSherbrooke.qc.ca/languesmodernes/604-HAE_Grammar_Practice/ I'm trying to decipher what...
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
8
by: CDARS | last post by:
Hi all, I have a confusing question on ASP.NET cookies usage: 1> Response.Cookies("test").value = Now 2> Response.Write(Request.Cookies("test").value) 3> 4> Response.write("<hr>") 5>...
6
by: Stephane | last post by:
Hi, I have a login page where if the user wants his access codes to be saved are set into a cookie. In the logout page, I want to delete those cookies. I tried this and this is not working at...
1
by: Alex Nitulescu | last post by:
I have the following very simple colde (while learning about cookies and session state): Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
0
by: Claudio | last post by:
I have a demo app where cookies does not work. The first page create a cookies. The second read the contet. If I browse the pages via IE6.0 the pages does not work. If I browse the pages via...
0
by: rn5a | last post by:
This is how I am creating & then reading cookies: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'create cookies Response.Cookies("UserName").Value = "Ron"...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.