You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by steve <st...@keptprivate.com> on 2001/05/17 18:51:39 UTC

Newbie question...

hi,

I'm new to servlets and velocity which I'm interested in using to build a fairly complex web app.  I'm not new to web apps... having done a
lot with C++ and ProC (Oracle) and also a custom
template language similar to Velocity.  The old stuff I worked on was 3-tier  Templates - Content Daemons - DB.  With Velocity the way that I
would like to go is to build the data-access into
the same velocity servlet.

Something like this:

In the handleRequest method parse the URL and
then call something that pushes all the necessary data into the context then load the right template... done.

I've got a sample Velocity servlet running, parsing the URL... at which point it occured to me that I have to do some database stuff, etc. to
load values into the context.  It is clear to
me that the DB connection init. etc. needs to be done in an "init" somewhere outside of handleRequest.

The question is where?  Can someone throw me
some tips? (or better some example code ;-)

Sorry if this is a stupid question... but as I said I'm a newbie to these techonlogies and I did search through the archive of this list and didn't find an answer.

Thanks

Steve

Re: Newbie question...

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
steve wrote:
> 
> hi,
> 
> I'm new to servlets and velocity which I'm interested in using to build a fairly complex web app.  I'm not new to web apps... having done a
> lot with C++ and ProC (Oracle) and also a custom
> template language similar to Velocity.  The old stuff I worked on was 3-tier  Templates - Content Daemons - DB.  With Velocity the way that I
> would like to go is to build the data-access into
> the same velocity servlet.
> 
> Something like this:
> 
> In the handleRequest method parse the URL and
> then call something that pushes all the necessary data into the context then load the right template... done.

That's how every webapp I have written with Velocity and WebMacro works
:)

> 
> I've got a sample Velocity servlet running, parsing the URL... at which point it occured to me that I have to do some database stuff, etc. to
> load values into the context.  It is clear to
> me that the DB connection init. etc. needs to be done in an "init" somewhere outside of handleRequest.
> 
> The question is where?  Can someone throw me
> some tips? (or better some example code ;-)
> 
> Sorry if this is a stupid question... but as I said I'm a newbie to these techonlogies and I did search through the archive of this list and didn't find an answer.

The first thing you will hear is to go take a look at Turbine :

  http://jakarta.apache.org/turbine/

It's a web app framework that does all this for you.  While I am quite
an accomplished 'wheelsmith' myself, reinvention gets boring, and these
guys do it right. :)

There are a lot of answers to the question actually.  You can use the
servlet init() method to do it for you, setup a connection pool for use
in a all servlets, etc....

You have to be careful here - unlike what you might naturally expect - a
servlet instance is usually shared among concurrent requests. 
Generally, a servlet runner (like Tomcat) will not make a pool of
servlets of a given class, but only one, and send every request through
on separate threads.  So initializing a db connection as a class member
will result in pain and pathos once you hit it with two users at the
same time. :)

Anyway, happy to talk about this (although it's a bit off topic for the
velocity list), but give Turbine a look-see.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
"still climbing up to the shoulders..."

Re: Newbie question...

Posted by Jacek Kempski <jk...@jacekkempski.com>.
Hi,

we call the database stuff early in the handleRequest, after doing some configuration (mapping of request parameters to a reasonable configuration of JDBC, imaging,
templates and so on - all through XML). But we call JDBC classes to configure themselves, so generally, outsource the database part from the Servlet. Anyway, this
comes from within handleRequest.


  public Template handleRequest (Context ctx){
....///set values from request
    resolve();
    setDataFactory();
    dataWorker = dataFactory.createWorker(workerClassName);
    dataWorkerOutputParams = dataWorker.work(dataWorkerInputParams);  //here comes JDBC
.....
}

hope this helps,

jack

steve schrieb:

> hi,
>
> I'm new to servlets and velocity which I'm interested in using to build a fairly complex web app.  I'm not new to web apps... having done a
> lot with C++ and ProC (Oracle) and also a custom
> template language similar to Velocity.  The old stuff I worked on was 3-tier  Templates - Content Daemons - DB.  With Velocity the way that I
> would like to go is to build the data-access into
> the same velocity servlet.
>
> Something like this:
>
> In the handleRequest method parse the URL and
> then call something that pushes all the necessary data into the context then load the right template... done.
>
> I've got a sample Velocity servlet running, parsing the URL... at which point it occured to me that I have to do some database stuff, etc. to
> load values into the context.  It is clear to
> me that the DB connection init. etc. needs to be done in an "init" somewhere outside of handleRequest.
>
> The question is where?  Can someone throw me
> some tips? (or better some example code ;-)
>
> Sorry if this is a stupid question... but as I said I'm a newbie to these techonlogies and I did search through the archive of this list and didn't find an answer.
>
> Thanks
>
> Steve