Hello all,
Does anyone have an example of running an application with multiple
servlets. So far everything I have seen has just one servlet. I am trying
to solve a design issue where I would like to create a session and have the
servlets in the application use that session.
If you can point me to examples I would appreciate it.
Joseph.
Hi Joseph,
as Session objects (from the Session API) are persistent they can be
accessed using the session id, regardless of which Servlet is being invoked.
Ie, you have a gateway servlet that sets the session up. All subsequent URLs
that you generate are processed using encodeURL() which will automatically
handle the adding of the session id for you (either as cookies or as a
parameter).
Any servlet that receives a valid session id can then access the Session
object into
which you have stored all sorts of useful information about the current
user.
The trick is to make sure that each servlet that is dependent on that
session object
is referred to by a URL that has been processed by encodeURL() or (for
redirects)
encodeRedirectURL(). Not to mention that the servlet has to sort out the
session tracking
in the normal way.
Once you have done this you can do some clever things with the session
object.
Say you have set your web site so that portions of the site are accessible
through a portal
that asks for User Name and Password. Some of your intrepid users might set
a bookmark to point
to a page within the protected section.
You can set your servlets up so that they check for a valid session object,
create one if needed,
if they have to create one redirect the user to the login page (with the new
session id encoded using
encodeRedirectURL()). You store the requested URL into session object and
the login servlet can then
send the user directly to that page after logging in.
This idea was shamelessly taken from "Java Servlet Programming" a book I
recommend thoroughly
Hope that points you in the right direction.
Andy Bailey
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html