tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: unable to set cookies
Date Mon, 04 Feb 2008 02:30:32 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Huragan,

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkemeMgACgkQ9CaO5/Lv0PA8DACeMglq8T8HhxZgJxL+AnT25mHk
sj0AoI3eGEEAaQ9mIJiNu3NgMrkjSxnB
=6EiW
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message