"Ravi Kumar.T" wrote:

> Is Data sharing possible with Servlet API 2.0.How data can be shared
> between servlets in JSDK 2.0 and how one servlet communicates with another
> servlet?Is it possible to call all public methods of a servlet from another
> servlet including init() and destroy() method?
>

In the 2.0 API, you can get a reference to another servlet
with ServletContext.getServlet(), and nothing stops you from calling the
init() and destroy() methods, or any other public method.  However, this is a
VERY bad idea, because the servlet will be in a different state that the
servlet engine thinks it is, and this will cause lots of problems.  In
addition, it won't work at all when you migrate to a 2.1 compliant servlet
engine later -- getServlet() has been deprecated and will always return NULL.

The easiest way to share data in a 2.0 servlet engine is to use an HttpSession
to store the data objects required by an individual user.  For objects that
are more global in scope, you can use the "singleton" pattern techniques
(static factory methods and such), but you need to be careful with your class
paths to make sure there is really only one copy -- the rule is to put classes
you want shared this way on the system class path for your servlet engine, not
in the directories or JAR files from which your servlets are loaded.

In the 2.1 API, ServletContext.setAttribute() and
ServletContext.getAttribute() have been added to facilitate data object
sharing between servlets that run in the same context.

Craig McClanahan

___________________________________________________________________________
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

Reply via email to