Home » Developer & Programmer » JDeveloper, Java & XML » Cookies
Cookies [message #92482] Sun, 08 August 2004 08:46 Go to next message
Bibudh Lahiri
Messages: 6
Registered: August 2004
Junior Member
Hi,i'm working with servlets on tomcat 5.0.i've run two servlets today to demonstrate working with cookies. The first one,SetCookies.java , is as follows.
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> public class SetCookies extends HttpServlet {
>   public void doGet(HttpServletRequest request, HttpServletResponse response)
>        throws ServletException, IOException {
>     Cookie myCookie = new Cookie("userID", "123");
>     response.addCookie(myCookie);
>     response.setContentType("text/html");
>    
>     PrintWriter out = response.getWriter();
>
>     HttpSession session = request.getSession(false);
>     String sessionId  = "";
>     if (session != null)
>     {
>       sessionId = session.getId();
>     }
>    
>     out.println ("<!DOCTYPE HTML PUBLIC "~//W3C//DTD HTML 4.0 " +
>      "Transitional//EN">n" +
>      "<HTML>n" +
>      "<HEAD><TITLE> My Cookie </TITLE></HEAD>n" +
>      "<BODY>n" +
>      "  " + myCookie + "n" +
>      " 

Cookie written
n" +
>      " 

Session ID ="+ sessionId +"
n"
>      );
>     if (session == null)
>     {
>       out.println("

session is null
n");
>     }
>     out.println("</BODY></HTML>");
>    }
> }
>
> As is clear from the code itself,it creates a cookie with name="userID" and value="123".
>
> The second servlet, ShowCookies.java, is as follows:
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class ShowCookies extends HttpServlet {
>   public void doGet(HttpServletRequest request, HttpServletResponse response)
>       throws ServletException, IOException {
>     response.setContentType("text/html");
>     HttpSession session = request.getSession(false);
>     String sessionId  = "";
>     if (session == null)
>     {
>       System.out.println("session is null");
>     }
>     else
>     {
>
>      sessionId = session.getId();
>     }
>    
>     PrintWriter out = response.getWriter();
>     out.println ("<!DOCTYPE HTML PUBLIC "~//W3C//DTD HTML 4.0 " +
>     "Transitional//EN">n" +
>      "<HTML>n" +
>      "<HEAD><TITLE> My Cookie </TITLE></HEAD>n" +
>      "<BODY>n" +
>      "

Session ID ="+ sessionId +"
n" +
>      " My Cookie n" +
>      "

");
>
>      Cookie[[]] cookies = request.getCookies();
>      if (cookies == null)
>      {
>        out.println("No cookies");
>      }
>      else
>      {
>        Cookie myCookie;
>        for(int i=0; i< 1; i++)
>        {
>          myCookie = cookies[[i]];
>          out.println(myCookie.getName() + "=" + myCookie.getValue());
>        }
>      }
>   }
> }

> ShowCookies gets the cookie and displays its name-value correctly.
>    Now my question is ,where i'm running the two servlets separately(i've even run them successfully on different browser windows),how does ShowCookies access the same cookie that is created by SetCookies? Also, for either servlet, the session object is null. Is the cookie generally placed in the session itself? If yes, then why is the session null in this case? i haven't got any file in the folder (that contains the servlets) that represents the cookie created. Then how are the cookies created and where are they stored? Can anyone suggest some reading material (online) that throws some light on cookies?
Re: Cookies [message #92483 is a reply to message #92482] Tue, 10 August 2004 09:31 Go to previous messageGo to next message
sai
Messages: 27
Registered: October 2001
Junior Member
Hi,

A cookie is a text file stored in your local directory. When I as a user request a page for the first time, the server sets some values and puts it in a text file in my local directory ( usually C:Documents and Settings$USERNAME$Cookies ). The name of the text file is of this type: blahiri@yahoo[[2]].txt When I request the page second time, data from the text file is sent to the web server.

This is how servlets know about cookies.

Cookies can be enabled/disabled from browser, Tools| Internet Options| Privacy | Advanced. Here you can override the defaults, and "block" cookies.

Cookies are sent via request. So, they are not stored in session.

Hope this answered your question. If not, write, and we'll try to give you an answer for you.

Thanks,
Sai
Re: Cookies [message #92484 is a reply to message #92483] Tue, 10 August 2004 23:25 Go to previous message
Bibudh Lahiri
Messages: 6
Registered: August 2004
Junior Member
Saikat,
i read ur answer and verified that the folder (specified by u) in my comp contains the cookies, as u mentioned. But one question still remains: i'd written that i'd run two separate servlets (SetCookies and ShowCookies) from two separate browser windows. Then how did the second servlet fetch exactly the same cookie which was created by the first? Aren't the request objects different for these two servlets? Pls reply.
Previous Topic: XML ORACLE and £
Next Topic: Cannot extract hour from date field (Oracle 10.1.0.2 with OCI jdbc driver)
Goto Forum:
  


Current Time: Sat Apr 20 09:32:40 CDT 2024