[mod_python] servlets and cookies

John Aherne johna at johnaherne.co.uk
Mon Nov 1 16:12:56 EST 2004

Hi all,

I am hoping someone can tell me what I am doing wrong. with setting 
cookies in the following code snippet.

I am using the mps servlet classes for calling scripts but for the 
cookies I use the mod_python cookie class

I have a simple bit of script that checks for a cookie and if not 
present will set it.

If I include the last line to redirect to another page, the script will 
not set the cookie and redirect to the next page.

If I comment out the last line to redirect to another page, the script 
will set the cookie - the browser is set to warn if a cookie is being 
set -  and I will see a message asking if I want to accept the cookie.

The redirected page checks for the cookie and displays a line if the 
cookie is found.

What I can't work out is why the cookie is not set if I uncomment the 
redirect line in the block that is supposed to add the cookie.

Code snippet follows:--

# -*- python -*-
from mod_python import Cookie
from mod_python import apache

from mod_python.servlet import *
from _HTML import *

class incook2(HTMLPage):
 title = 'JAH table test'
 css_links = ['jah.css']
# query_vars = ['action']

 def write_content(self):
  checkout = Cookie.get_cookies(self.req)
  if checkout.has_key('user'):
      check = Cookie.Cookie('user','niven')
      Cookie.add_cookie(self.req, check)

  I hope someone can give me a clue as to what the problem is.

John Aherne


More information about the Mod_python mailing list