Wyn Easton wrote:

> Hello,
>
> I have been reading everything I could find about
> ServletContext and have not been able to find an
> example of how to share a servlet in one web
> application with servlets in other web applications.
>
> I can get a RequestDispather for a servlet in another
> web application, but when I "forward" to that servlet
> I loose the session I had.
>
> Is there a way that the servlet I forward to in
> another
> web application could be considered part of my web
> application so I can keep the same session?
>
> Thanks.
> [...]

Hi :-)

*0 the following is a good email :-)

------------------------------<good
email>----------------------------------
On Fri, 9 Mar 2001, Richard Yumul wrote:

> Hi all -
> I've been playing with servlets for over a year or so now, and one of
the
> drawback's I've encountered is the ability for separate WAR's to
communicate
> w/ each other. For example, say I have two WAR's: one that handles
logging
> in (after a successful login, it puts user info in a HttpSession), and
one
> which handles a simple form submission. The URL's would be deployed
> something like (in TOMCAT):
>
> <webapps>/login
> <webapps>/form
>
> Now, is there any way for the 'form' app too access the user info from
the
> 'login' context?  I know they're in separate ServletContext's, but
this
> doesn't seem like an unreasonable functionality to ask for.  Or, if
there's
> any workaround, or alternate method to achieve this type of
functionality
> I'd very much appreciate it if somebody would enlighten me.
>
> The only option that I see, if I want multiple app's talking to each
other
> (like a calendar, message board, or news feed, for example) would be
to
> bundle them all in one WAR.  Thus you can't deploy the individual
app's
> separately.
>
> If I'm missing the bigger picture, somebody please enlighten me. :)

Thought off the top of my head: Will crossContext help with this?
That's a property of contexts that you set up in server.xml.
According to the comments there:

   ... crossContext=true (allows you to access other contexts via
   ServletContext.getContext())

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
------------------------------</good
email>----------------------------------



*1 the following is from Servlet API doc:

-  getRequestDispatcher
public RequestDispatcher getRequestDispatcher(java.lang.String path)
Returns a RequestDispatcher object that acts as a wrapper for the
resource located at the given path. A RequestDispatcher object can be
used to forward a request to the resource or to include the resource in
a response. The resource can be dynamic or static.
The pathname must begin with a "/" and is interpreted as relative to the
current context root. Use getContext to obtain a RequestDispatcher for
resources in foreign contexts. This method returns null if the
ServletContext cannot return a RequestDispatcher.
Parameters:
path - a String specifying the pathname to the resource
Returns:
a RequestDispatcher object that acts as a wrapper for the resource at
the specified path

- getContext
public ServletContext getContext(java.lang.String uripath)
Returns a ServletContext object that corresponds to a specified URL on
the server.
This method allows servlets to gain access to the context for various
parts of the server, and as needed obtain RequestDispatcher objects from
the context. The given path must be absolute (beginning with "/") and is
interpreted based on the server's document root.
In a security conscious environment, the servlet container may return
null for a given URL.
Parameters:
uripath - a String specifying the absolute URL of a resource on the
server
Returns:
the ServletContext object that corresponds to the named URL

*2 so I think:
   - if we want "goto" another Servlet within the same webapp:
     ServletContext thisctx=this.getServletContext();
     RequestDispatcher
rdpt=thisctx.getRequestDispatcher("/servlet/MyServlet0");
      rdpt.forward(req, res);


   - if we want to "goto" another Servlet within another webapp(but the
two
      webapps are in the same Servlet container):
     //from webapp0-MyServlet0 to webapp1-MyServlet1
     ...
     ServletContext
thatctx=this.getServletContext().getContext("/webapp1");
     RequestDispatcher
rdpt=thatctx.getRequestDispatcher("/servlet/MyServlet1");
      rdpt.forward(req, res);
     ...

*3 and you also can "share" object "across" these 2 webapps(in the same
Servlet
   Conainer):
   - in webapp0-MyServlet0, put a object in ServletContext
   ...
   Object Obj=new Object();
   this.getServletContext().setAttribute("Obj", Obj);
   System.out.println("in MyServlet0, Obj.hashCode()="+Obj.hashCode());
   System.out.println("in MyServlet0,
this.ctx.hashCode()="+this.getServletContext().hashCode());
   ...

   - in webapp1-MyServlet1, get that object from ServletContext
   ...
      ServletContext
thatctx=this.getServletContext().getContext("/webapp0");
      Object thatObj=thatctx.getAttribute("Obj");
      System.out.println("in MyServlet1,
thatctx.hashCode()="+thatctx.hashCode());
      System.out.println("in MyServlet1,
thatObj.hashCode()="+thatObj.hashCode());
      System.out.println("in MyServlet1,
this.ctx.hashCode()="+this.getServletContext().hashCode());
   ...

*4 I didn't consider the security issue in the above.

*5 I guess another "non-standard" way to share object "across" webapp
   is:
   use a SingleTon style Helper class, it is loaded by a classlaoder
which
    is "upper than" webapp classloader: for example, Shared classloader
    (by putting MyHelper.jar in TOMCAT_HOME/lib with TC4.0),
    or System classloader(by putting MyHelper.jar in CLASSPATH
     with TC3.x or TC4.0?)


Bo
Mar.21, 2001


Reply via email to