You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Todd Wright <tw...@bbn.com> on 2001/11/02 17:25:23 UTC

Embedding Tomcat with intra-app Servlets

I'd like to embed Tomcat within my Java application, but where some Servlet 
requests would *directly* call into my running application's code (+data).   
The current "EmbededTomcat" support seems to be aimed at launching a 
stand-alone Tomcat that only supports file contexts, with no callback support 
for access to the launching application.

I'm thinking of Tomcat 3.3, 'though 4.0 could be used if required.

For example, suppose I have a calendar application that I'd like to add web 
support.  Inside my application I have all the data I need, and I'd like my 
servlets to directly access my the data structures.  Flattening the data out 
to an external database/filesystem is not an option.  All Servlet requests 
would funnel into my application so I can call my own (inner) Servlet.   I 
still want Tomcat to do the HTTP parsing, SSL, sessions, Servlet-API, etc.   
My application must load the internal Servlet instance.

Can I use an interceptor, or (if needed) pull apart "EmbededTomcat"?  Is 
there an example of how to do this?  I hope it's clear why this would be a 
very useful embedding scenario...

Pseudocode:

    public class MyApp implements Runnable {

       /** launcher */
       public static void main(String[] args) {
           // launch!
          (new MyApp()).run();
       }

       public void run() {
          // create my internal Servlet before Tomcat is launched
          //
          // this inner class is defined later in this method
          Servlet myServlet = new MyServlet();

          // launch Tomcat
          EmbeddedTomcat et = new EmbeddedTomcat();
          et.setArgs(new String[] {"start"});
          et.execute();

          //
          // hand-waving here!   Could be moved to before the "et.execute()".
          //
          insert myServlet into Tomcat to handle all "/*" requests

          // 
          //  I now want "myServlet" to be called with all "/*" requests
          // 

          // run forever in this example...
       }

       /**
        * sample internal data method.
        *
        * MyServlet will call this method
        */
       public String getInternalString() {
          return "foo";
       }

       /** 
        * My inner servlet -- has access back into "MyApp"  
        *  instance (non-static).
        */
       private class MyServlet extends HttpServlet {
          public void doGet(
              HttpServletRequest request, 
              HttpServletResponse response)
                   throws IOException, ServletException  {
           response.setContentType("text/html");
           PrintWriter out = response.getWriter();
           out.println("<html><body>Internal String is ");

           // access internal MyApp method/data!
           out.println(getInternalString());

           out.println("</body></html>");
       }
    }

Thanks!
   Todd

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Embedding Tomcat with intra-app Servlets

Posted by Todd Wright <tw...@bbn.com>.
On Friday 02 November 2001 16:08, Craig R. McClanahan wrote:
> On Fri, 2 Nov 2001, Todd Wright wrote:
> > Date: Fri, 2 Nov 2001 15:56:44 -0400
> > From: Todd Wright <tw...@bbn.com>
> > Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> > To: Tomcat Users List <to...@jakarta.apache.org>
> > Subject: Re: Embedding Tomcat with intra-app Servlets
> >
> >
> > I'm not sure this solves my problem...
> >
> > If I pull my Servlet out of my application's code then the instance will
> > be constructed and loaded by Tomcat as usual, but then the Servlet can't
> > access my application's internal data.  I need the Servlet to have a
> > callback into my application for it to obtain the data required for
> > generating the response.
>
> What's wrong with passing the relevant stuff with static variables?  If
> your application classes are visible to webapps (by virtue of being in the
> "lib" directory), these static variables and all the correpsonding classes
> are visible to both.

Yeah, I thought of that -- it feels like a big kluge.  This is what I'm doing 
for now, but I was hoping that there's a cleaner way to do this.  The 
downside of the statics it that I can only have one Tomcat running, or I have 
to toy with the classloaders, plus it's ugly.  As-is I need to mess with the 
Tomcat classloaders, since my application has its own bootstrapper.

The other option I heard (from Frank Lawlor) was to create Bean-like data 
structures to pass data, but I don't see how this would interface with my 
application.  It would clean up the API between the front-end JSPs and the 
app, but I'd still need my app to serve up these data structures.  I'd still 
need a primitive (internal) HTTP server to wire in the backend.  Maybe I'm 
not understanding his suggestion.

> > As a more complex example, imagine creating an HTTP front-end to, say, a
> > Java-based text editor.  For the Servlet to see what's happening within
> > the editor -- which file is being edited, where's the cursor, etc --it'll
> > need direct access the editor's data structures.  Spilling and updating
> > this data to a shared file/database/whatever would be wasteful; in my
> > intended application there's 100+ megs of constantly changing data that
> > the Servlet may need to examine when generating its response.
>
> It's your app, so architect it the way you want ... but I would do it
> differently (essentially backwards from the way you are looking at it).
> I would start by designing a standard web application in which your
> application logic is embedded.  Then, the only issue becomes, how do I
> start the web container up when I needed it?  That's easy to manage, using
> either Bootstrap.main() or the embedded class.

Well, I'm replacing a custom-build HTTP 1.0 server on a 5-year-old project, 
so I can't restructure things that way.  We have about a million lines of 
code, most of which has no direct interaction with the web serving.

Besides, why should Tomcat force my application that way.  There are other 
modules that we have integrated (JNDI, RMI, JDBC, Swing, etc) that don't make 
the application revolve around them.

Sorry if that sounded blunt...  I'd like to embed Tomcat cleanly, but it 
seems overly awkward.   The pseudocode I provided seems like a natural 
"callback" way to embed an HTTP server into an application.

Todd

>
> > Todd
>
> Craig
>
> > On Friday 02 November 2001 14:10, Craig R. McClanahan wrote:
> > > Here's the Tomcat 4 approach -- 3.x has similar things but I'm not as
> > > familiar with the details.
> > >
> > > You have two fundamental choices, based on whether you want to use
> > > server.xml to configure Tomcat or not:
> > >
> > > * To use server.xml, you just need to set up an environment
> > >   like the "catalina.sh" script does, and call
> > >
> > >     Bootstrap.main("start");
> > >
> > >   in a separate thread.  When you ultimately want to shut things
> > >   down, call:
> > >
> > >     Bootstrap.main("stop");
> > >
> > > * To configure all the components yourself, you will use the
> > >   org.apache.catalina.startup.Embedded class (similar to
> > >   EmbeddedTomcat in 3.x).  This class includes a dummy main()
> > >   program that demonstrates how you do things.
> > >
> > > For actually processing the requests yourself, you would only need a
> > > single webapp (the ROOT webapp) with a web.xml that configures your
> > > servlet to handle all requests (i.e. a <servlet-mapping> with a pattern
> > > of "/*").  There's no need for interceptors (3.x) or valves (4.x) to do
> > > this.
> > >
> > > I would recommend you implement the application logic as a standard
> > > servlet, in its own class, and separate the startup/shutdown issues out
> > > to their own class.  There is nothing to be gained by combining them.
> > >
> > > Craig
> > >
> > > On Fri, 2 Nov 2001, Todd Wright wrote:
> > > > Date: Fri, 2 Nov 2001 12:25:23 -0400
> > > > From: Todd Wright <tw...@bbn.com>
> > > > Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> > > > To: tomcat-user@jakarta.apache.org
> > > > Subject: Embedding Tomcat with intra-app Servlets
> > > >
> > > >
> > > > I'd like to embed Tomcat within my Java application, but where some
> > > > Servlet requests would *directly* call into my running application's
> > > > code (+data). The current "EmbededTomcat" support seems to be aimed
> > > > at launching a stand-alone Tomcat that only supports file contexts,
> > > > with no callback support for access to the launching application.
> > > >
> > > > I'm thinking of Tomcat 3.3, 'though 4.0 could be used if required.
> > > >
> > > > For example, suppose I have a calendar application that I'd like to
> > > > add web support.  Inside my application I have all the data I need,
> > > > and I'd like my servlets to directly access my the data structures. 
> > > > Flattening the data out to an external database/filesystem is not an
> > > > option.  All Servlet requests would funnel into my application so I
> > > > can call my own (inner) Servlet.   I still want Tomcat to do the HTTP
> > > > parsing, SSL, sessions, Servlet-API, etc. My application must load
> > > > the internal Servlet instance.
> > > >
> > > > Can I use an interceptor, or (if needed) pull apart "EmbededTomcat"? 
> > > > Is there an example of how to do this?  I hope it's clear why this
> > > > would be a very useful embedding scenario...
> > > >
> > > > Pseudocode:
> > > >
> > > >     public class MyApp implements Runnable {
> > > >
> > > >        /** launcher */
> > > >        public static void main(String[] args) {
> > > >            // launch!
> > > >           (new MyApp()).run();
> > > >        }
> > > >
> > > >        public void run() {
> > > >           // create my internal Servlet before Tomcat is launched
> > > >           //
> > > >           // this inner class is defined later in this method
> > > >           Servlet myServlet = new MyServlet();
> > > >
> > > >           // launch Tomcat
> > > >           EmbeddedTomcat et = new EmbeddedTomcat();
> > > >           et.setArgs(new String[] {"start"});
> > > >           et.execute();
> > > >
> > > >           //
> > > >           // hand-waving here!   Could be moved to before the
> > > > "et.execute()". //
> > > >           insert myServlet into Tomcat to handle all "/*" requests
> > > >
> > > >           //
> > > >           //  I now want "myServlet" to be called with all "/*"
> > > > requests //
> > > >
> > > >           // run forever in this example...
> > > >        }
> > > >
> > > >        /**
> > > >         * sample internal data method.
> > > >         *
> > > >         * MyServlet will call this method
> > > >         */
> > > >        public String getInternalString() {
> > > >           return "foo";
> > > >        }
> > > >
> > > >        /**
> > > >         * My inner servlet -- has access back into "MyApp"
> > > >         *  instance (non-static).
> > > >         */
> > > >        private class MyServlet extends HttpServlet {
> > > >           public void doGet(
> > > >               HttpServletRequest request,
> > > >               HttpServletResponse response)
> > > >                    throws IOException, ServletException  {
> > > >            response.setContentType("text/html");
> > > >            PrintWriter out = response.getWriter();
> > > >            out.println("<html><body>Internal String is ");
> > > >
> > > >            // access internal MyApp method/data!
> > > >            out.println(getInternalString());
> > > >
> > > >            out.println("</body></html>");
> > > >        }
> > > >     }
> > > >
> > > > Thanks!
> > > >    Todd
> > > >
> > > > --
> > > > To unsubscribe:   <ma...@jakarta.apache.org>
> > > > For additional commands: <ma...@jakarta.apache.org>
> > > > Troubles with the list: <ma...@jakarta.apache.org>
> > >
> > > --
> > > To unsubscribe:   <ma...@jakarta.apache.org>
> > > For additional commands: <ma...@jakarta.apache.org>
> > > Troubles with the list: <ma...@jakarta.apache.org>
> >
> > --
> > To unsubscribe:   <ma...@jakarta.apache.org>
> > For additional commands: <ma...@jakarta.apache.org>
> > Troubles with the list: <ma...@jakarta.apache.org>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Embedding Tomcat with intra-app Servlets

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 2 Nov 2001, Todd Wright wrote:

> Date: Fri, 2 Nov 2001 15:56:44 -0400
> From: Todd Wright <tw...@bbn.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: Tomcat Users List <to...@jakarta.apache.org>
> Subject: Re: Embedding Tomcat with intra-app Servlets
>
>
> I'm not sure this solves my problem...
>
> If I pull my Servlet out of my application's code then the instance will be
> constructed and loaded by Tomcat as usual, but then the Servlet can't access
> my application's internal data.  I need the Servlet to have a callback into
> my application for it to obtain the data required for generating the response.
>

What's wrong with passing the relevant stuff with static variables?  If
your application classes are visible to webapps (by virtue of being in the
"lib" directory), these static variables and all the correpsonding classes
are visible to both.

> As a more complex example, imagine creating an HTTP front-end to, say, a
> Java-based text editor.  For the Servlet to see what's happening within the
> editor -- which file is being edited, where's the cursor, etc --it'll need
> direct access the editor's data structures.  Spilling and updating this data
> to a shared file/database/whatever would be wasteful; in my intended
> application there's 100+ megs of constantly changing data that the Servlet
> may need to examine when generating its response.
>

It's your app, so architect it the way you want ... but I would do it
differently (essentially backwards from the way you are looking at it).
I would start by designing a standard web application in which your
application logic is embedded.  Then, the only issue becomes, how do I
start the web container up when I needed it?  That's easy to manage, using
either Bootstrap.main() or the embedded class.

> Todd
>

Craig



> On Friday 02 November 2001 14:10, Craig R. McClanahan wrote:
> > Here's the Tomcat 4 approach -- 3.x has similar things but I'm not as
> > familiar with the details.
> >
> > You have two fundamental choices, based on whether you want to use
> > server.xml to configure Tomcat or not:
> >
> > * To use server.xml, you just need to set up an environment
> >   like the "catalina.sh" script does, and call
> >
> >     Bootstrap.main("start");
> >
> >   in a separate thread.  When you ultimately want to shut things
> >   down, call:
> >
> >     Bootstrap.main("stop");
> >
> > * To configure all the components yourself, you will use the
> >   org.apache.catalina.startup.Embedded class (similar to
> >   EmbeddedTomcat in 3.x).  This class includes a dummy main()
> >   program that demonstrates how you do things.
> >
> > For actually processing the requests yourself, you would only need a
> > single webapp (the ROOT webapp) with a web.xml that configures your
> > servlet to handle all requests (i.e. a <servlet-mapping> with a pattern of
> > "/*").  There's no need for interceptors (3.x) or valves (4.x) to do this.
> >
> > I would recommend you implement the application logic as a standard
> > servlet, in its own class, and separate the startup/shutdown issues out to
> > their own class.  There is nothing to be gained by combining them.
> >
> > Craig
> >
> > On Fri, 2 Nov 2001, Todd Wright wrote:
> > > Date: Fri, 2 Nov 2001 12:25:23 -0400
> > > From: Todd Wright <tw...@bbn.com>
> > > Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> > > To: tomcat-user@jakarta.apache.org
> > > Subject: Embedding Tomcat with intra-app Servlets
> > >
> > >
> > > I'd like to embed Tomcat within my Java application, but where some
> > > Servlet requests would *directly* call into my running application's code
> > > (+data). The current "EmbededTomcat" support seems to be aimed at
> > > launching a stand-alone Tomcat that only supports file contexts, with no
> > > callback support for access to the launching application.
> > >
> > > I'm thinking of Tomcat 3.3, 'though 4.0 could be used if required.
> > >
> > > For example, suppose I have a calendar application that I'd like to add
> > > web support.  Inside my application I have all the data I need, and I'd
> > > like my servlets to directly access my the data structures.  Flattening
> > > the data out to an external database/filesystem is not an option.  All
> > > Servlet requests would funnel into my application so I can call my own
> > > (inner) Servlet.   I still want Tomcat to do the HTTP parsing, SSL,
> > > sessions, Servlet-API, etc. My application must load the internal Servlet
> > > instance.
> > >
> > > Can I use an interceptor, or (if needed) pull apart "EmbededTomcat"?  Is
> > > there an example of how to do this?  I hope it's clear why this would be
> > > a very useful embedding scenario...
> > >
> > > Pseudocode:
> > >
> > >     public class MyApp implements Runnable {
> > >
> > >        /** launcher */
> > >        public static void main(String[] args) {
> > >            // launch!
> > >           (new MyApp()).run();
> > >        }
> > >
> > >        public void run() {
> > >           // create my internal Servlet before Tomcat is launched
> > >           //
> > >           // this inner class is defined later in this method
> > >           Servlet myServlet = new MyServlet();
> > >
> > >           // launch Tomcat
> > >           EmbeddedTomcat et = new EmbeddedTomcat();
> > >           et.setArgs(new String[] {"start"});
> > >           et.execute();
> > >
> > >           //
> > >           // hand-waving here!   Could be moved to before the
> > > "et.execute()". //
> > >           insert myServlet into Tomcat to handle all "/*" requests
> > >
> > >           //
> > >           //  I now want "myServlet" to be called with all "/*" requests
> > >           //
> > >
> > >           // run forever in this example...
> > >        }
> > >
> > >        /**
> > >         * sample internal data method.
> > >         *
> > >         * MyServlet will call this method
> > >         */
> > >        public String getInternalString() {
> > >           return "foo";
> > >        }
> > >
> > >        /**
> > >         * My inner servlet -- has access back into "MyApp"
> > >         *  instance (non-static).
> > >         */
> > >        private class MyServlet extends HttpServlet {
> > >           public void doGet(
> > >               HttpServletRequest request,
> > >               HttpServletResponse response)
> > >                    throws IOException, ServletException  {
> > >            response.setContentType("text/html");
> > >            PrintWriter out = response.getWriter();
> > >            out.println("<html><body>Internal String is ");
> > >
> > >            // access internal MyApp method/data!
> > >            out.println(getInternalString());
> > >
> > >            out.println("</body></html>");
> > >        }
> > >     }
> > >
> > > Thanks!
> > >    Todd
> > >
> > > --
> > > To unsubscribe:   <ma...@jakarta.apache.org>
> > > For additional commands: <ma...@jakarta.apache.org>
> > > Troubles with the list: <ma...@jakarta.apache.org>
> >
> > --
> > To unsubscribe:   <ma...@jakarta.apache.org>
> > For additional commands: <ma...@jakarta.apache.org>
> > Troubles with the list: <ma...@jakarta.apache.org>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Embedding Tomcat with intra-app Servlets

Posted by Todd Wright <tw...@bbn.com>.
I'm not sure this solves my problem...

If I pull my Servlet out of my application's code then the instance will be 
constructed and loaded by Tomcat as usual, but then the Servlet can't access 
my application's internal data.  I need the Servlet to have a callback into 
my application for it to obtain the data required for generating the response.

As a more complex example, imagine creating an HTTP front-end to, say, a 
Java-based text editor.  For the Servlet to see what's happening within the 
editor -- which file is being edited, where's the cursor, etc --it'll need 
direct access the editor's data structures.  Spilling and updating this data 
to a shared file/database/whatever would be wasteful; in my intended 
application there's 100+ megs of constantly changing data that the Servlet 
may need to examine when generating its response.

Todd

On Friday 02 November 2001 14:10, Craig R. McClanahan wrote:
> Here's the Tomcat 4 approach -- 3.x has similar things but I'm not as
> familiar with the details.
>
> You have two fundamental choices, based on whether you want to use
> server.xml to configure Tomcat or not:
>
> * To use server.xml, you just need to set up an environment
>   like the "catalina.sh" script does, and call
>
>     Bootstrap.main("start");
>
>   in a separate thread.  When you ultimately want to shut things
>   down, call:
>
>     Bootstrap.main("stop");
>
> * To configure all the components yourself, you will use the
>   org.apache.catalina.startup.Embedded class (similar to
>   EmbeddedTomcat in 3.x).  This class includes a dummy main()
>   program that demonstrates how you do things.
>
> For actually processing the requests yourself, you would only need a
> single webapp (the ROOT webapp) with a web.xml that configures your
> servlet to handle all requests (i.e. a <servlet-mapping> with a pattern of
> "/*").  There's no need for interceptors (3.x) or valves (4.x) to do this.
>
> I would recommend you implement the application logic as a standard
> servlet, in its own class, and separate the startup/shutdown issues out to
> their own class.  There is nothing to be gained by combining them.
>
> Craig
>
> On Fri, 2 Nov 2001, Todd Wright wrote:
> > Date: Fri, 2 Nov 2001 12:25:23 -0400
> > From: Todd Wright <tw...@bbn.com>
> > Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> > To: tomcat-user@jakarta.apache.org
> > Subject: Embedding Tomcat with intra-app Servlets
> >
> >
> > I'd like to embed Tomcat within my Java application, but where some
> > Servlet requests would *directly* call into my running application's code
> > (+data). The current "EmbededTomcat" support seems to be aimed at
> > launching a stand-alone Tomcat that only supports file contexts, with no
> > callback support for access to the launching application.
> >
> > I'm thinking of Tomcat 3.3, 'though 4.0 could be used if required.
> >
> > For example, suppose I have a calendar application that I'd like to add
> > web support.  Inside my application I have all the data I need, and I'd
> > like my servlets to directly access my the data structures.  Flattening
> > the data out to an external database/filesystem is not an option.  All
> > Servlet requests would funnel into my application so I can call my own
> > (inner) Servlet.   I still want Tomcat to do the HTTP parsing, SSL,
> > sessions, Servlet-API, etc. My application must load the internal Servlet
> > instance.
> >
> > Can I use an interceptor, or (if needed) pull apart "EmbededTomcat"?  Is
> > there an example of how to do this?  I hope it's clear why this would be
> > a very useful embedding scenario...
> >
> > Pseudocode:
> >
> >     public class MyApp implements Runnable {
> >
> >        /** launcher */
> >        public static void main(String[] args) {
> >            // launch!
> >           (new MyApp()).run();
> >        }
> >
> >        public void run() {
> >           // create my internal Servlet before Tomcat is launched
> >           //
> >           // this inner class is defined later in this method
> >           Servlet myServlet = new MyServlet();
> >
> >           // launch Tomcat
> >           EmbeddedTomcat et = new EmbeddedTomcat();
> >           et.setArgs(new String[] {"start"});
> >           et.execute();
> >
> >           //
> >           // hand-waving here!   Could be moved to before the
> > "et.execute()". //
> >           insert myServlet into Tomcat to handle all "/*" requests
> >
> >           //
> >           //  I now want "myServlet" to be called with all "/*" requests
> >           //
> >
> >           // run forever in this example...
> >        }
> >
> >        /**
> >         * sample internal data method.
> >         *
> >         * MyServlet will call this method
> >         */
> >        public String getInternalString() {
> >           return "foo";
> >        }
> >
> >        /**
> >         * My inner servlet -- has access back into "MyApp"
> >         *  instance (non-static).
> >         */
> >        private class MyServlet extends HttpServlet {
> >           public void doGet(
> >               HttpServletRequest request,
> >               HttpServletResponse response)
> >                    throws IOException, ServletException  {
> >            response.setContentType("text/html");
> >            PrintWriter out = response.getWriter();
> >            out.println("<html><body>Internal String is ");
> >
> >            // access internal MyApp method/data!
> >            out.println(getInternalString());
> >
> >            out.println("</body></html>");
> >        }
> >     }
> >
> > Thanks!
> >    Todd
> >
> > --
> > To unsubscribe:   <ma...@jakarta.apache.org>
> > For additional commands: <ma...@jakarta.apache.org>
> > Troubles with the list: <ma...@jakarta.apache.org>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Embedding Tomcat with intra-app Servlets

Posted by "Craig R. McClanahan" <cr...@apache.org>.
Here's the Tomcat 4 approach -- 3.x has similar things but I'm not as
familiar with the details.

You have two fundamental choices, based on whether you want to use
server.xml to configure Tomcat or not:

* To use server.xml, you just need to set up an environment
  like the "catalina.sh" script does, and call

    Bootstrap.main("start");

  in a separate thread.  When you ultimately want to shut things
  down, call:

    Bootstrap.main("stop");

* To configure all the components yourself, you will use the
  org.apache.catalina.startup.Embedded class (similar to
  EmbeddedTomcat in 3.x).  This class includes a dummy main()
  program that demonstrates how you do things.

For actually processing the requests yourself, you would only need a
single webapp (the ROOT webapp) with a web.xml that configures your
servlet to handle all requests (i.e. a <servlet-mapping> with a pattern of
"/*").  There's no need for interceptors (3.x) or valves (4.x) to do this.

I would recommend you implement the application logic as a standard
servlet, in its own class, and separate the startup/shutdown issues out to
their own class.  There is nothing to be gained by combining them.

Craig


On Fri, 2 Nov 2001, Todd Wright wrote:

> Date: Fri, 2 Nov 2001 12:25:23 -0400
> From: Todd Wright <tw...@bbn.com>
> Reply-To: Tomcat Users List <to...@jakarta.apache.org>
> To: tomcat-user@jakarta.apache.org
> Subject: Embedding Tomcat with intra-app Servlets
>
>
> I'd like to embed Tomcat within my Java application, but where some Servlet
> requests would *directly* call into my running application's code (+data).
> The current "EmbededTomcat" support seems to be aimed at launching a
> stand-alone Tomcat that only supports file contexts, with no callback support
> for access to the launching application.
>
> I'm thinking of Tomcat 3.3, 'though 4.0 could be used if required.
>
> For example, suppose I have a calendar application that I'd like to add web
> support.  Inside my application I have all the data I need, and I'd like my
> servlets to directly access my the data structures.  Flattening the data out
> to an external database/filesystem is not an option.  All Servlet requests
> would funnel into my application so I can call my own (inner) Servlet.   I
> still want Tomcat to do the HTTP parsing, SSL, sessions, Servlet-API, etc.
> My application must load the internal Servlet instance.
>
> Can I use an interceptor, or (if needed) pull apart "EmbededTomcat"?  Is
> there an example of how to do this?  I hope it's clear why this would be a
> very useful embedding scenario...
>
> Pseudocode:
>
>     public class MyApp implements Runnable {
>
>        /** launcher */
>        public static void main(String[] args) {
>            // launch!
>           (new MyApp()).run();
>        }
>
>        public void run() {
>           // create my internal Servlet before Tomcat is launched
>           //
>           // this inner class is defined later in this method
>           Servlet myServlet = new MyServlet();
>
>           // launch Tomcat
>           EmbeddedTomcat et = new EmbeddedTomcat();
>           et.setArgs(new String[] {"start"});
>           et.execute();
>
>           //
>           // hand-waving here!   Could be moved to before the "et.execute()".
>           //
>           insert myServlet into Tomcat to handle all "/*" requests
>
>           //
>           //  I now want "myServlet" to be called with all "/*" requests
>           //
>
>           // run forever in this example...
>        }
>
>        /**
>         * sample internal data method.
>         *
>         * MyServlet will call this method
>         */
>        public String getInternalString() {
>           return "foo";
>        }
>
>        /**
>         * My inner servlet -- has access back into "MyApp"
>         *  instance (non-static).
>         */
>        private class MyServlet extends HttpServlet {
>           public void doGet(
>               HttpServletRequest request,
>               HttpServletResponse response)
>                    throws IOException, ServletException  {
>            response.setContentType("text/html");
>            PrintWriter out = response.getWriter();
>            out.println("<html><body>Internal String is ");
>
>            // access internal MyApp method/data!
>            out.println(getInternalString());
>
>            out.println("</body></html>");
>        }
>     }
>
> Thanks!
>    Todd
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>