|
Daniel Popowich
dpopowich at comcast.net
Wed Feb 2 08:38:22 EST 2005
Colin Doherty writes:
> Hi,
>
> I'm having difficulty getting sessions working in
> modpython.servlet, the tutorial example works so it
> would appear to be an error in my code rather than
> apache/mod_python set up:
You have a few things you can do here...
> from mod_python.servlet import Servlet
>
> class mpseg(Servlet):
> __msg = ''
> auth_realm = 'Private'
>
> def __init__(self):
> Servlet.__init__(self)
>
> def auth(self):
> if self.session.is_new():
> user,pw = self._get_user_pw()
> if user == 'user' and pw == 'password':
> self.__msg = 'Hi'
> return
> else:
> self._unauthorized()
> if not self.session.is_new():
> self.__msg = 'Welcome back'
>
> def respond(self):
> self.writeln(self.__msg)
> return True
>
> def prep(self):
> use_session = True
As you've written it in prep, the variable use_session is local, so as
far as the servlet is concerned, it's not set. You want:
def prep(self):
self.use_session = True
Servlet.prep(self)
Note, if you write your own prep, you HAVE to call the superclass'
prep. Typically, you would call the superclass prep first, but since
you're setting a variable Servlet.prep needs, you have to set the
variable before calling it.
Better yet, unless you want to make using sessions dynamic per
request, you'd be better off setting it in the class definition so
each instance inherits it:
class mpseg(Servlet):
__msg = ''
auth_realm = 'Private'
use_session = True
def respond(self):
...
Then you don't even have to write your own prep method if all it was
doing was setting the use_session variable. Likewise, you don't have
to write __init__ if all it's doing is calling Servlet.__init__.
Double check this:
pydoc mod_python.servlet.Servlet.prep
and this:
http://lnx1.blue-fox.com/~dpopowich/mpstutorial/lifecycle
Cheers,
Daniel Popowich
-----------------------------------------------
http://home.comcast.net/~d.popowich/mpservlets/
|