Hi:
Your servlet just demonstrated how the Cookie object works.
The servlet is as server side. It send cookie to client and stored it at
client's
local desk. When client make any new request, it will always attach the
cookies
at the http head, so server side can use it to identify the client. It is
way Cookies
are used in session tracking.
When your servlet create the cookie at the servlet, the res object would not
able to
get it yet. Only when this cookie has been sent to client, then at next time
client will
send it back to server at its http head. This is why, you got it when you
refresh it.
hope this will clear the problem you have.
yi
-----Original Message-----
From: anurag dewan [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, February 13, 2001 8:17 AM
To: [EMAIL PROTECTED]
Subject: Newbie: problem in cookie and servlet
hi,
I am trying to create a servlet that sets a cookie and then
prints the name of the cookie.
The code is given below.
the servlet runs fine but does not give the name of the cookie the first
time.
Only when i refresh it gives the name of the cookie.
Please help me out.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class imp extends HttpServlet{
public void init(ServletConfig config)
throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
String name;
res.addCookie(new Cookie("session_id","anurag"));
Cookie[] cookies = req.getCookies();
res.setContentType("text/html");
PrintWriter out =res.getWriter();
out.println("<HTML>");
out.println("<BODY>");
if (cookies != null)
{
for(int i=0;i < cookies.length;i++)
{
name=cookies[i].getName();
out.println(name);
}
}
out.println("the cookies r displayed");
out.println("</body></html>");
out.close();
}
}
Thanks
-anurag
____________________________________________________________________
Get free email and a permanent address at http://www.netaddress.com/?N=1
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html