tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: unable to set cookies
Date Mon, 04 Feb 2008 02:30:32 GMT
Hash: SHA1


Huragan wrote:
| I am new to j2ee and have been trying to understand how cookies work.

Cookies are independent of the J2EE specification: they are part of the
HTTP specification. The servlet specification (part of J2EE) says that
cookies are one supported way of client identification for session tracking.

| I narrowed it down to the part of code where cookies get retrieved.
| There are no cookies being returned with the response object. When i
| displayed the header info there were no cookies being set at all.

How did you observe this?

| As you can see the code works if i comment out the cookies retrieval.

If you say so.

| However, I still tried to set it to true in the context.xml file.
| which didn't work.

Setting cookies="true" on context.xml only affects the use of cookies
for session handling. It does not affect your application's ability to
use cookies for other reasons.

| Then tried to set a context element in Host in server.xml. Needless
| to say that didn't work either.

Yeah, don't do that.

| Now am at my wits end as to why it is not working. i am attaching the
| servlet code below:

Let's see...

| 	public void doGet(HttpServletRequest request,
| 					  HttpServletResponse response)
| 		throws ServletException, IOException {
| 		Cookie c1 = new Cookie("userName", "Sandeep");
| 		Cookie c2 = new Cookie("password", "password");
| 		response.addCookie(c1);
| 		response.addCookie(c2);
| 		response.setContentType("text/html");
| 		PrintWriter out = response.getWriter();
| 		out.println("<HTML>");
| 		out.println("<HEAD>");
| 		out.println("<TITLE>Cookie Test</TITLE>");
| 		out.println("</HEAD>");
| 		out.println("<BODY>");
| 		out.println("Please click on the button to see the " +
| 					"cookies sent to you.");
| 		out.println("<BR>");
| 		out.println("<FORM METHOD=POST>");
| 		out.println("<INPUT TYPE=SUBMIT VALUE=SUBMIT>");
| 		out.println("</FORM>");
| 		out.println("</BODY>");
| 		out.println("</HTML>");
| 	}

So, this doesn't send any cookies? What if you flush and close the
output writer?

| 		Cookie[] cookies = request.getCookies();
| 		int length = cookies.length;

Since the documentation for request.getCookies says that NULL can be
returned, you ought to check for NULL before trying to do something with
your "cookies" reference.

- -chris
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla -


To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message