Hi... first, although it is not really relevant to your question, you do not
need to explicitly import the javax.servlet.http and javax.servlet packages
into a jsp; they are automatically included. At first glance your code
looks OK, but I have never attempted to use the "setDomain" and "setPath"
methods when writing and retrieving cookies. I suspect that your problem
lies there. I would suggest that you try to take the SIMPLEST approach first
and see if that works. That is, just write the cookie and attempt to read it
back WITHOUT doing the "setDomain" and "setPath" stuff. If that works, then
you know where your problem lies.
-----Original Message-----
From: Mazin Salih [mailto:mbsalih@h...]
Sent: Friday, October 18, 2002 7:17 AM
To: Pro_JavaServer_Pages
Subject: [pro_jsp] writing cookies/jsp
Hi everyone,
I am trying to write and issue a cookie on one page and then retrieving it
in another page but its not working can anyone help?
I am using the following code to write:
<HTML>
<HEAD><TITLE> issue cookie </TITLE>
<BODY>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="java.io.*"%>
<%
Cookie c = new Cookie ("firstname","mazin");
c.setMaxAge(3600);
c.setDomain(".scitse.wlv.ac.uk:7777");
c.setPath("/");
response.addCookie(c);
response.setContentType("text/html");
%>
</BODY>
</HTML>
and the following to retrieve:
<HTML>
<HEAD><TITLE> get cookie </TITLE>
<BODY>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="java.io.*"%>
<%
Cookie[] cookies = request.getCookies ();
response.setContentType("text/html");
if (cookies!=null){
for (int i=0; i<cookies.length;i++){
Cookie c = cookies[i];
String name = c.getName();
String value = c.getValue();
int version = c.getVersion();
out.println(name + " = " + value);
}
}
%>
</BODY>
</HTML>
---------- Internet E-mail Confidentiality Disclaimer ----------
PRIVILEGED / CONFIDENTIAL INFORMATION may be contained in this message. If
you are not the addressee indicated in this message or the employee or agent
responsible for delivering it to the addressee, you are hereby on notice
that you are in possession of confidential and privileged information. Any
dissemination, distribution, or copying of this e-mail is strictly
prohibited. In such case, you should destroy this message and kindly notify
the sender by reply e-mail. Please advise immediately if you or your
employer do not consent to Internet email for messages of this kind.
Opinions, conclusions, and other information in this message that do not
relate to the official business of my firm shall be understood as neither
given nor endorsed by it.