This is the method as described in the Sun Java documentation and tutorials. I
think if more people on this board took the time to digest these documents, then
these topics wouldn't be such a mistery. I don't understand where you get the
notion that casting a servlet is any different than casting any other kind of java

type. What possible "legitimate reason" might casting fail because it's a servlet?

Servlets are fundamentally no different that any other type of Java class.

The reason getServlet() was deprecated, as well as the SessionContext was for
security reasons (so that scope is protected). So the method I described is THE
legitimate, fully sanctioned by Sun, method of servlet communication.

-ernie

"K.V. Chandrinos" wrote:

> I am in doubt if both these techniques are GUARANTEED to work because they
> involve servlet casting. Unless the point raised for example by Jason
> Hunter in his book is false, this casting might fail for quite legitimate
> reasons. Am I right? Or does this set/getAttribute() methodology mimicks
> an interface implementation, hence by-passing the problem?
>
> While you are at it, pls. be kind enough to comment on the following
> (rather academic) question:
>
> Since the specs allow a servlet runner implementation in which several
> contexts are created (e.g. for load balancing) how can one get all the
> servlets currently loaded? Quite naturally, any servlet can only query on
> its own servlet context. Is there a server.getServletContext*s*()  missing
> or am I missing something?
>
> Thank you,
> Kostas
>
> P.S. Would you know of a servlet runner that has elected to implement this
> 'feature' of various contexts?
>
> On Wed, 16 Feb 2000, Mark Foster wrote:
>
> > I think that example may only work for JSDK >= 2.1
> > Using JSDK 2.0 here is what I do...
> >
> >    servletobj s = (servletobj)getServletContext().getServlet("servletobj");
> >    String foo = s.someMethod().toString();
> >
> > It's too bad getServlet() was deprecated, because the technique described
> > below  seems alot more clunky.
> > -mdf
> >
> > -----Original Message-----
> > From: Ernie V [mailto:[EMAIL PROTECTED]]
> > Sent: Wednesday, February 16, 2000 6:43 AM
> > To: [EMAIL PROTECTED]
> > Subject: Re: How can i call a servlet method from another servlet
> >
> >
> > To call a servlet from another servlet, you need to get a reference to the
> > servlet that you want to call by using the ServletContext. The servlet that
> > you want to call from other servlets must put a reference to itself in the
> > servlet context. Use getAttribute() and getAttribute() methods to accomplish
> > this. This is usually done in the init() method of the callable servlet:
> >  public void init(ServletConfig cfg) throws ServletException
> >  {
> >   super.init(cfg);
> >   // Add this servlet to the context
> >   getServletContext().setAttribute("callableServlet", this);
> >  }
> > Then put this in the calling servlet:
> > CallableServlet cs =
> >     (CallableServlet)getServletContext().getAttribute("callableServlet");
> > Now you can use the reference "cs" to invoke methods in the callable
> > servlet.
> > -ernie
> >
> > ___________________________________________________________________________
> > 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
> >
>
> ___________________________________________________________________________
> 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
begin:vcard
n:Varitimos;Ernie
tel;cell:781-929-3856
tel;home:781-784-1997
tel;work:781-784-3900
x-mozilla-html:TRUE
url:http://www.skyserver.com
org:Skyserver Consulting, Inc.
adr:;;144 Upland Rd;Sharon;MA;02067;
version:2.1
email;internet:[EMAIL PROTECTED]
title:President
fn:Ernie Varitimos
end:vcard

Reply via email to