You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Todd O'Bryan <to...@mac.com> on 2004/02/06 11:27:37 UTC

Forwarding a getTemplate request

I've set up a single servlet to handle all requests for my webapp, 
hopefully avoiding massive changes to web.xml.

How do I forward things to other VelocityViewServlets in there?

Since handleRequest() is an instance method, I have to have an instance 
to call it with, but I'm pretty sure I don't want to explicitly create 
a new instance since I'd have a bunch of them floating around when I 
probably don't need them. Or maybe I do want to explicitly use new.

Anyway, maybe I'm doing something terribly wrong. My code looks 
something like this:

	public Template handleRequest(HttpServletRequest request,
									HttpServletResponse response,
									Context ctx) throws Exception {
		String path = request.getPathInfo();
		if (path.equals("login.html")) {
			return getTemplate("templates/login.vm");
		} else if (path.equals("menu.html")){
			//would like to call the handleRequest method in a MenuServlet class 
here
		} else {
			return getTemplate("templates/error.vm");
		}
	}

TIA,
Todd


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Dave Newton <da...@solaraccess.com>.
On Fri, 2004-02-06 at 19:46, Todd O'Bryan wrote:
> For some reason, it's easier for me to write if-else statements than 
> disconnected XML elements that need to appear twice (once to name the 
> servlet and then again to map it). Maybe I'm just weird.

Undoubtedly ;) Sounds like it's a pretty custom app that only you will
ever look at, so it probably doesn't matter anyway :)

> the mornings when I can get up that early. Spending that time 
> programming means that much of my grading goes on the back burner. And 
> I get paid about $34,000/year. Don't you wish you were me?

Been there, done that ;) And I was making less than that until yesterday
anyway (around $0/year) for six months. Hrmph. Then all of a sudden I
have four interviews, two jobs, and another possibility 3000 miles away.

I was probably happier (if hungrier) sitting on me buttocks.

> I've had those, too...as you can probably tell.

Ah, but now you get to have a lovely relaxing weekend... grading.

Hrm.

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Todd O'Bryan <to...@mac.com>.
On Feb 6, 2004, at 9:02 AM, Dave Newton wrote:

> Unfortunately I can't answer your question, so you can feel free to
> ignore all of this :D but...
>
> On Fri, 2004-02-06 at 05:27, Todd O'Bryan wrote:
>> I've set up a single servlet to handle all requests for my webapp,
>> hopefully avoiding massive changes to web.xml.
>
> So instead you'll make massive changes to your servlet source?
>
For some reason, it's easier for me to write if-else statements than 
disconnected XML elements that need to appear twice (once to name the 
servlet and then again to map it). Maybe I'm just weird.

>> public Template handleRequest(HttpServletRequest request,
>>                              HttpServletResponse response,
>>                              Context ctx) throws Exception {
>>    String path = request.getPathInfo();
>>    if (path.equals("login.html")) {
>>        return getTemplate("templates/login.vm");
>>    } else if (path.equals("menu.html")){
>>        //would like to call the handleRequest method in a MenuServlet 
>> class here
>>    } else {
>>        return getTemplate("templates/error.vm");
>>    }
>> }
>
> (Note: tabs are evil :)
>
Sorry.

> I'm not necessarily saying that you should use struts or another
> framework, but having to change a servlet if you add/delete a page, or
> want to test with a different version of a page, etc. seems rather
> cumbersome and a lot more error prone than changing a config file or
> some sort!
>
I probably should be using some kind of framework. But I don't have 
time to learn one. I need to get a couple of things written ASAP to use 
with the high school classes I teach. Add to that the fact that, after 
a day of teaching, I'm generally not in any shape to do any kind of 
programming, and that means my productive time is limited to 4am-6am on 
the mornings when I can get up that early. Spending that time 
programming means that much of my grading goes on the back burner. And 
I get paid about $34,000/year. Don't you wish you were me?

> Sorry, it's a crabby, dreary Friday morning and I'm off to a
> soul-sucking consult ;)
>
> Dave
>
I've had those, too...as you can probably tell.

>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Nathan Bubna <na...@esha.com>.
mr.x said:
...
> Thanks for the advice.. I posted a message some time ago about this issue.
> Nobody reported that the method exists.
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg11165.html

sorry.  we can't catch them all.  that's why we have docs, javadocs, and
source code openly available online.

> Anyway.. I'm currently happy with my solution.

glad to hear it!

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: Forwarding a getTemplate request

Posted by "mr.x" <mr...@tele2.ch>.
Hi Nathan 

Thanks for the advice.. I posted a message some time ago about this issue.
Nobody reported that the method exists. 
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg11165.html

Anyway.. I'm currently happy with my solution. 

Rolf

-----Original Message-----
From: Nathan Bubna [mailto:nathan@esha.com] 
Sent: Freitag, 6. Februar 2004 20:26
To: Velocity Users List
Subject: Re: Forwarding a getTemplate request


mr.x said:
...
> I stopped using the VelocityViewServlet. The servlet uses the 
> singleton "approach" of velocity. For me it was easier to create my 
> own servlet which creates an instance of velocity. // create velocity 
> engine ve = new VelocityEngine();
> Furthermore the "ve" can be "asked" if a template exists. (not possible in
> the VelocityViewServlet - i think)
...

not true.  Velocity.templateExists(String name) works just fine with the
VVS. the only difference between singleton (Velocity) and non-singleton
(VelocityEngine) approaches is that you can configure different
VelocityEngine's differently and pass them around.  Using
org.apache.velocity.app.Velocity, on the other hand, you can have only one
velocity configuration and you have static access to the same methods as
VelocityEngine.

we plan to look into switching VelocityViewServlet to the non-singleton use
pattern after VelocityTools 1.1 final is released.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Nathan Bubna <na...@esha.com>.
mr.x said:
...
> I stopped using the VelocityViewServlet. The servlet uses the singleton
> "approach" of velocity. For me it was easier to create my own servlet which
> creates an instance of velocity.
> // create velocity engine
> ve = new VelocityEngine();
> Furthermore the "ve" can be "asked" if a template exists. (not possible in
> the VelocityViewServlet - i think)
...

not true.  Velocity.templateExists(String name) works just fine with the VVS.
the only difference between singleton (Velocity) and non-singleton
(VelocityEngine) approaches is that you can configure different
VelocityEngine's differently and pass them around.  Using
org.apache.velocity.app.Velocity, on the other hand, you can have only one
velocity configuration and you have static access to the same methods as
VelocityEngine.

we plan to look into switching VelocityViewServlet to the non-singleton use
pattern after VelocityTools 1.1 final is released.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Nathan Bubna <na...@esha.com>.
Todd O'Bryan said:
> On Feb 6, 2004, at 8:27 PM, Nathan Bubna wrote:
> > Todd O'Bryan said:
...
> >> • VelocityViewServlet class - standalone servlet that renders Velocity
> >> templates. Invoked directly from web clients requests, or via servlet
> >> forwarding similar to how JSP files are rendered by JSPServlet.
> >
> > this just means that you can map servlet requests to Velocity
> > templates, so you don't need to do weirdness like:
> >
> >>>>    String path = request.getPathInfo();
> >>>>    if (path.equals("login.html")) {
> >>>>        return getTemplate("templates/login.vm");
> >
> So, is it really just me who thinks this web.xml idea of having to name
> servlets and map them to particular URLs (IN COMPLETELY SEPARATE
> SECTIONS OF THE WEB.XML FILE!!!) is just a mess?

a few responses:
-mapping servlets to particular URLs is a bad idea IMHO.  i prefer sticking
with the standard approach of mapping servlets to particular URL *patterns*.
-hardcoding paths and template names in java makes portability a pain and
means recompiling to add new templates/pages.  not fun.
-how big is your web.xml that your concerned about naming/defining the
servlets and specifying their path mappings in another?
-this whole servlet/web.xml thing was pretty darn popular last i checked, so
yeah,
you're in the minority. :)

> I find that I'm much more likely to make a silly mistake writing the
> web.xml file than I am writing Java code, and that once I've made that
> mistake, I'm much less likely to get any decent debugging information.
> I wrote a small webapp using the web.xml pattern, and the confusion of
> having my program flow logic separated from my program code was the
> source of great frustration.

don't put flow logic into your web.xml, that's not what it's for.

> I'd like to use a single servlet to figure out what's being requested,
> and then pass along that request appropriately. If all I need is a
> Template with minimal Context information, I can throw what I need to
> into the context and return the appropriate Template. If I need more
> extensive handling, I'd like to be able to call the handleRequest()
> method (or a similarly functional method) of another servlet and let it
> do its thing.
>
> But, since handleRequest() is an instance method, I need an instance.
> Now, if I specified the servlet (again, twice!) in the web.xml file,
> the servlet container would automatically instantiate it for me. Since
> I'm not doing that, would I have to instantiate it myself, and, if so,
> how would I do that exactly?

really, it sounds like you should be using a framework.  but if you really
want to pass things from one servlet to another, then you have to use
RequestDispatcher.  trying to get your own instances of servlets is a bad
idea.

> I think it's clear that I haven't grasped the way this whole
> contraption is supposed to work. Or maybe I have, and I've just
> convinced myself that it can't be as kludgy and clumsy as it really is,
> so I think I'm missing something basic when in fact I've got it. If the
> latter, I'm wasting my time trying to figure out how to code a webapp
> so that I don't have a lot of VelocityViewServlet subclasses with four
> or five line long handleRequest() methods in them. I'm trying to
> coalesce code and include a bunch of similar requests together. Is that
> not a good idea? Is it just not done?

ideally you would just be using just one VelocityViewServlet (or subclass) to
render your templates.  to populate your template's context, you would use the
toolbox support for common things, and some sort of framework to handle page
specific "actions", where the framework would dispatch the request to the
VelocityViewServlet after it had done it's thing.

> Sorry I'm being so dense. I promise it's not intentional.

:)

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Tom Jackson <to...@rmadilo.com>.
On Fri, 2004-02-06 at 19:49, Todd O'Bryan wrote:

> So, is it really just me who thinks this web.xml idea of having to name 
> servlets and map them to particular URLs (IN COMPLETELY SEPARATE 
> SECTIONS OF THE WEB.XML FILE!!!) is just a mess?

With a one person development team it probably doesn't matter too much,
but I have worked with 'request processors' for a while, and being able
to change the logic of what program runs in a configuration file is much
better than hacking code. Any time you are hard coding values into a
program, you should step back and reconsider. 

tom jackson


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Claude Brisson <cl...@savoirweb.com>.
Todd O'Bryan wrote :
> If I need more 
> extensive handling, I'd like to be able to call the handleRequest() 
> method (or a similarly functional method) of another servlet and let it 
> do its thing.

Have you considered inheriting servlets ?

CloD


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Todd O'Bryan <to...@mac.com>.
I have downloaded Maverick and it's as if the scales dropped from my 
eyes. This is the kind of thing I was trying unsuccessfully to do 
myself and doing a crappy job at. All the controller code kind of lives 
in the maverick.xml file, but it seems less disjointed and yet more 
powerful than the simple web.xml. And I still get to use VelocityView 
without ever having to subclass it myself.

Thanks all for the pointers. It just goes to show that much frustration 
can be averted by finding out what you need *before* you actually need 
it. :-)

Todd

On Feb 7, 2004, at 11:56 AM, Nathan Bubna wrote:
>
> +1  and really, the simpler frameworks like maverick shouldn't take 
> too long
> to learn.  and by all reports, maverick works fine with VelocityView.
>
> Nathan Bubna
> nathan@esha.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Will Glass-Husain <wg...@forio.com>.
> actually, the VelocityViewServlet's approach is called "Pull" :)

How embarassing :-)

Maybe we should work up some type of description of the different options
for the wiki.  Help new users to understand how to get started.

WILL

----- Original Message ----- 
From: "Nathan Bubna" <na...@esha.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Saturday, February 07, 2004 8:56 AM
Subject: Re: Forwarding a getTemplate request


> Will Glass-Husain said:
> > There's basically two approaches to working with Velocity.
> >
> > (1) The "Push" approach where all pages are basically treated the same.
> >         (a) put some standard tools and information in the context
> >         (b) decide what page to return based on the URL.
> >
> > I've found this a conceptually easy approach.  This is the approach
taken by
> > VelocityViewServlet, which simply looks for a template based on the URL
> > path.
>
> actually, the VelocityViewServlet's approach is called "Pull" :)
>
> > It's analogous to a static web site, except you get to include
> > dynamic info (say data from a database).   Just put the info in the
context,
> > and design all the pages with Velocity.  It gets problematic when you
start
> > having special pages with different behavior.  (for example, redirecting
to
> > a login page or responding to a form).  Every new action you have to
take,
> > or every special case page you display needs an if statements in the
main
> > handleRequest method (whatever is called).
>
> to be more specific, the problem is when the site needs to be more
interactive
> and not just data-presentation oriented.  at this point, you need to
> supplement the VelocityViewServlet with either a framework to handle
"actions"
> or, for smaller sites, a tool like Velosurf.
>
> > As far as I know, with the default VelocityViewServlet approach, you
only
> > need to map /*.htm to the servlet in web.xml and you'll be fine.  You
don't
> > need to map each page separately.
> >
> > (2) The "Framework" approach which allows for sites with more complex
> > navigation logic
> >         (a) use a special servlet that handles all incoming requests
> >         (b) the servlet uses some type of config file to map requests to
> > Velocity templates
> >         (c) the servlet uses some type of logic (usually custom Java
> > classes) to decide what to put into the context
> >         (d) there are usually various tools to help with security,
> > responding to form actions, etc.
> >
> > My favorite here is Maverick.  Probably the most straightforward to use
and
> > the least overhead in terms of structure.  You define each page in an
XML
> > file and write an Action Java class.  The benefit is that (A) you can
put
> > stuff in the context only for a certain page and (B) you can define
> > responses to a form that is submitted.  It sounds like a pain to write a
> > Java class for each page, but it turns out not to be so big a deal,
> > especially if you use a superclass for common features.  Velocity also
works
> > well with Turbine, which provides many more tools (but is more complex).
> > And Nathan & co have done a nice job of providing a bridge between
Velocity
> > and Struts.
> >
> > http://mav.sourceforge.net/
> > l
> > As a side note, many people try to roll their own framework for a
project.
> > The prevailing wisdom is that in most of the cases you'll save time and
have
> > a more reliable system by using an already built framework with an
existing
> > user base.
>
> +1  and really, the simpler frameworks like maverick shouldn't take too
long
> to learn.  and by all reports, maverick works fine with VelocityView.
>
> Nathan Bubna
> nathan@esha.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Nathan Bubna <na...@esha.com>.
Will Glass-Husain said:
> There's basically two approaches to working with Velocity.
>
> (1) The "Push" approach where all pages are basically treated the same.
>         (a) put some standard tools and information in the context
>         (b) decide what page to return based on the URL.
>
> I've found this a conceptually easy approach.  This is the approach taken by
> VelocityViewServlet, which simply looks for a template based on the URL
> path.

actually, the VelocityViewServlet's approach is called "Pull" :)

> It's analogous to a static web site, except you get to include
> dynamic info (say data from a database).   Just put the info in the context,
> and design all the pages with Velocity.  It gets problematic when you start
> having special pages with different behavior.  (for example, redirecting to
> a login page or responding to a form).  Every new action you have to take,
> or every special case page you display needs an if statements in the main
> handleRequest method (whatever is called).

to be more specific, the problem is when the site needs to be more interactive
and not just data-presentation oriented.  at this point, you need to
supplement the VelocityViewServlet with either a framework to handle "actions"
or, for smaller sites, a tool like Velosurf.

> As far as I know, with the default VelocityViewServlet approach, you only
> need to map /*.htm to the servlet in web.xml and you'll be fine.  You don't
> need to map each page separately.
>
> (2) The "Framework" approach which allows for sites with more complex
> navigation logic
>         (a) use a special servlet that handles all incoming requests
>         (b) the servlet uses some type of config file to map requests to
> Velocity templates
>         (c) the servlet uses some type of logic (usually custom Java
> classes) to decide what to put into the context
>         (d) there are usually various tools to help with security,
> responding to form actions, etc.
>
> My favorite here is Maverick.  Probably the most straightforward to use and
> the least overhead in terms of structure.  You define each page in an XML
> file and write an Action Java class.  The benefit is that (A) you can put
> stuff in the context only for a certain page and (B) you can define
> responses to a form that is submitted.  It sounds like a pain to write a
> Java class for each page, but it turns out not to be so big a deal,
> especially if you use a superclass for common features.  Velocity also works
> well with Turbine, which provides many more tools (but is more complex).
> And Nathan & co have done a nice job of providing a bridge between Velocity
> and Struts.
>
> http://mav.sourceforge.net/
> l
> As a side note, many people try to roll their own framework for a project.
> The prevailing wisdom is that in most of the cases you'll save time and have
> a more reliable system by using an already built framework with an existing
> user base.

+1  and really, the simpler frameworks like maverick shouldn't take too long
to learn.  and by all reports, maverick works fine with VelocityView.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Will Glass-Husain <wg...@forio.com>.
Hi Todd,

There's basically two approaches to working with Velocity.

(1) The "Push" approach where all pages are basically treated the same.
        (a) put some standard tools and information in the context
        (b) decide what page to return based on the URL.

I've found this a conceptually easy approach.  This is the approach taken by
VelocityViewServlet, which simply looks for a template based on the URL
path.  It's analogous to a static web site, except you get to include
dynamic info (say data from a database).   Just put the info in the context,
and design all the pages with Velocity.  It gets problematic when you start
having special pages with different behavior.  (for example, redirecting to
a login page or responding to a form).  Every new action you have to take,
or every special case page you display needs an if statements in the main
handleRequest method (whatever is called).

As far as I know, with the default VelocityViewServlet approach, you only
need to map /*.htm to the servlet in web.xml and you'll be fine.  You don't
need to map each page separately.

(2) The "Framework" approach which allows for sites with more complex
navigation logic
        (a) use a special servlet that handles all incoming requests
        (b) the servlet uses some type of config file to map requests to
Velocity templates
        (c) the servlet uses some type of logic (usually custom Java
classes) to decide what to put into the context
        (d) there are usually various tools to help with security,
responding to form actions, etc.

My favorite here is Maverick.  Probably the most straightforward to use and
the least overhead in terms of structure.  You define each page in an XML
file and write an Action Java class.  The benefit is that (A) you can put
stuff in the context only for a certain page and (B) you can define
responses to a form that is submitted.  It sounds like a pain to write a
Java class for each page, but it turns out not to be so big a deal,
especially if you use a superclass for common features.  Velocity also works
well with Turbine, which provides many more tools (but is more complex).
And Nathan & co have done a nice job of providing a bridge between Velocity
and Struts.

http://mav.sourceforge.net/
l
As a side note, many people try to roll their own framework for a project.
The prevailing wisdom is that in most of the cases you'll save time and have
a more reliable system by using an already built framework with an existing
user base.

Hope this is a useful summary.

WILL



----- Original Message ----- 
From: "Todd O'Bryan" <to...@mac.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Friday, February 06, 2004 7:49 PM
Subject: Re: Forwarding a getTemplate request



On Feb 6, 2004, at 8:27 PM, Nathan Bubna wrote:

> Todd O'Bryan said:
>> Thanks. I'll take the code, of course. But I got the idea from the
>> VelocityView website where it says:
>>
>> • VelocityViewServlet class - standalone servlet that renders Velocity
>> templates. Invoked directly from web clients requests, or via servlet
>> forwarding similar to how JSP files are rendered by JSPServlet.
>
> this just means that you can map servlet requests to Velocity
> templates, so
> you don't need to do weirdness like:
>
>>>>    String path = request.getPathInfo();
>>>>    if (path.equals("login.html")) {
>>>>        return getTemplate("templates/login.vm");
>
>

So, is it really just me who thinks this web.xml idea of having to name
servlets and map them to particular URLs (IN COMPLETELY SEPARATE
SECTIONS OF THE WEB.XML FILE!!!) is just a mess?

I find that I'm much more likely to make a silly mistake writing the
web.xml file than I am writing Java code, and that once I've made that
mistake, I'm much less likely to get any decent debugging information.
I wrote a small webapp using the web.xml pattern, and the confusion of
having my program flow logic separated from my program code was the
source of great frustration.

I'd like to use a single servlet to figure out what's being requested,
and then pass along that request appropriately. If all I need is a
Template with minimal Context information, I can throw what I need to
into the context and return the appropriate Template. If I need more
extensive handling, I'd like to be able to call the handleRequest()
method (or a similarly functional method) of another servlet and let it
do its thing.

But, since handleRequest() is an instance method, I need an instance.
Now, if I specified the servlet (again, twice!) in the web.xml file,
the servlet container would automatically instantiate it for me. Since
I'm not doing that, would I have to instantiate it myself, and, if so,
how would I do that exactly?

I think it's clear that I haven't grasped the way this whole
contraption is supposed to work. Or maybe I have, and I've just
convinced myself that it can't be as kludgy and clumsy as it really is,
so I think I'm missing something basic when in fact I've got it. If the
latter, I'm wasting my time trying to figure out how to code a webapp
so that I don't have a lot of VelocityViewServlet subclasses with four
or five line long handleRequest() methods in them. I'm trying to
coalesce code and include a bunch of similar requests together. Is that
not a good idea? Is it just not done?

Sorry I'm being so dense. I promise it's not intentional.
Todd


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Todd O'Bryan <to...@mac.com>.
On Feb 6, 2004, at 8:27 PM, Nathan Bubna wrote:

> Todd O'Bryan said:
>> Thanks. I'll take the code, of course. But I got the idea from the
>> VelocityView website where it says:
>>
>> • VelocityViewServlet class - standalone servlet that renders Velocity
>> templates. Invoked directly from web clients requests, or via servlet
>> forwarding similar to how JSP files are rendered by JSPServlet.
>
> this just means that you can map servlet requests to Velocity 
> templates, so
> you don't need to do weirdness like:
>
>>>>    String path = request.getPathInfo();
>>>>    if (path.equals("login.html")) {
>>>>        return getTemplate("templates/login.vm");
>
>

So, is it really just me who thinks this web.xml idea of having to name 
servlets and map them to particular URLs (IN COMPLETELY SEPARATE 
SECTIONS OF THE WEB.XML FILE!!!) is just a mess?

I find that I'm much more likely to make a silly mistake writing the 
web.xml file than I am writing Java code, and that once I've made that 
mistake, I'm much less likely to get any decent debugging information. 
I wrote a small webapp using the web.xml pattern, and the confusion of 
having my program flow logic separated from my program code was the 
source of great frustration.

I'd like to use a single servlet to figure out what's being requested, 
and then pass along that request appropriately. If all I need is a 
Template with minimal Context information, I can throw what I need to 
into the context and return the appropriate Template. If I need more 
extensive handling, I'd like to be able to call the handleRequest() 
method (or a similarly functional method) of another servlet and let it 
do its thing.

But, since handleRequest() is an instance method, I need an instance. 
Now, if I specified the servlet (again, twice!) in the web.xml file, 
the servlet container would automatically instantiate it for me. Since 
I'm not doing that, would I have to instantiate it myself, and, if so, 
how would I do that exactly?

I think it's clear that I haven't grasped the way this whole 
contraption is supposed to work. Or maybe I have, and I've just 
convinced myself that it can't be as kludgy and clumsy as it really is, 
so I think I'm missing something basic when in fact I've got it. If the 
latter, I'm wasting my time trying to figure out how to code a webapp 
so that I don't have a lot of VelocityViewServlet subclasses with four 
or five line long handleRequest() methods in them. I'm trying to 
coalesce code and include a bunch of similar requests together. Is that 
not a good idea? Is it just not done?

Sorry I'm being so dense. I promise it's not intentional.
Todd


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Nathan Bubna <na...@esha.com>.
Todd O'Bryan said:
> Thanks. I'll take the code, of course. But I got the idea from the
> VelocityView website where it says:
>
> • VelocityViewServlet class - standalone servlet that renders Velocity
> templates. Invoked directly from web clients requests, or via servlet
> forwarding similar to how JSP files are rendered by JSPServlet.

this just means that you can map servlet requests to Velocity templates, so
you don't need to do weirdness like:

> >>    String path = request.getPathInfo();
> >>    if (path.equals("login.html")) {
> >>        return getTemplate("templates/login.vm");

but i'm guessing you're asking how to forward servlet requests.  (note that
i'm guessing because i'm at a loss to figure out what you're really trying to
do and why).  forwarding servlet requests is part of the servlet API, it has
no dependence on velocity at all.  in particular, you'll want to look into the
javax.servlet.RequestDispatcher class.

> So, I immediately googled for "velocity servlet forwarding" and I got
> the page where I found the mention, and that seems to be it.
>
> Where is this servlet forwarding hiding?

in servlet specification, documentation, and javadoc.  it's kinda sneaky that
way. :)

> Do I want to do something like
>
>      handleRequest(req, res, ctx) {
>          return new WhateverServlet().handleRequest();
>      }
>
> or is that (as I suspect) just really wrong?

the latter.

Nathan Bubna
nathan@esha.com


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Tom Jackson <to...@rmadilo.com>.
On Fri, 2004-02-06 at 16:56, Todd O'Bryan wrote:

> Where is this servlet forwarding hiding?

As Nathan said, the Servlet specification. I'm new to Java, like a week,
I am finding the Servlet spec very fine reading: 

http://jcp.org/aboutJava/communityprocess/final/jsr154/index.html

tom jackson


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Todd O'Bryan <to...@mac.com>.
Thanks. I'll take the code, of course. But I got the idea from the 
VelocityView website where it says:

• VelocityViewServlet class - standalone servlet that renders Velocity 
templates. Invoked directly from web clients requests, or via servlet 
forwarding similar to how JSP files are rendered by JSPServlet.

So, I immediately googled for "velocity servlet forwarding" and I got 
the page where I found the mention, and that seems to be it.

Where is this servlet forwarding hiding?

Do I want to do something like

     handleRequest(req, res, ctx) {
         return new WhateverServlet().handleRequest();
     }

or is that (as I suspect) just really wrong?

TIA,
Todd


On Feb 6, 2004, at 2:02 PM, mr.x wrote:

> Hi i also us a controller Servlet which then calls individual handlers 
> and
> templates. If an "action" (insert/update/delete ...) is done, i 
> redirect to
> a result page (if no errors where found).
>
> I stopped using the VelocityViewServlet. The servlet uses the singleton
> "approach" of velocity. For me it was easier to create my own servlet 
> which
> creates an instance of velocity.
> // create velocity engine
> ve = new VelocityEngine();
> Furthermore the "ve" can be "asked" if a template exists. (not 
> possible in
> the VelocityViewServlet - i think)
>
> I can send you some classes if you are interested.
>
>
> -----Original Message-----
> From: Dave Newton [mailto:dave@solaraccess.com]
> Sent: Freitag, 6. Februar 2004 15:03
> To: Velocity Users List
> Subject: Re: Forwarding a getTemplate request
>
>
> Unfortunately I can't answer your question, so you can feel free to 
> ignore
> all of this :D but...
>
> On Fri, 2004-02-06 at 05:27, Todd O'Bryan wrote:
>> I've set up a single servlet to handle all requests for my webapp,
>> hopefully avoiding massive changes to web.xml.
>
> So instead you'll make massive changes to your servlet source?
>
>> public Template handleRequest(HttpServletRequest request,
>>                              HttpServletResponse response,
>>                              Context ctx) throws Exception {
>>    String path = request.getPathInfo();
>>    if (path.equals("login.html")) {
>>        return getTemplate("templates/login.vm");
>>    } else if (path.equals("menu.html")){
>>        //would like to call the handleRequest method in a MenuServlet
> class here
>>    } else {
>>        return getTemplate("templates/error.vm");
>>    }
>> }
>
> (Note: tabs are evil :)
>
> I'm not necessarily saying that you should use struts or another 
> framework,
> but having to change a servlet if you add/delete a page, or want to 
> test
> with a different version of a page, etc. seems rather cumbersome and a 
> lot
> more error prone than changing a config file or some sort!
>
> Sorry, it's a crabby, dreary Friday morning and I'm off to a 
> soul-sucking
> consult ;)
>
> Dave
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


RE: Forwarding a getTemplate request

Posted by "mr.x" <mr...@tele2.ch>.
Hi i also us a controller Servlet which then calls individual handlers and
templates. If an "action" (insert/update/delete ...) is done, i redirect to
a result page (if no errors where found).

I stopped using the VelocityViewServlet. The servlet uses the singleton
"approach" of velocity. For me it was easier to create my own servlet which
creates an instance of velocity. 
// create velocity engine
ve = new VelocityEngine();
Furthermore the "ve" can be "asked" if a template exists. (not possible in
the VelocityViewServlet - i think)

I can send you some classes if you are interested.


-----Original Message-----
From: Dave Newton [mailto:dave@solaraccess.com] 
Sent: Freitag, 6. Februar 2004 15:03
To: Velocity Users List
Subject: Re: Forwarding a getTemplate request


Unfortunately I can't answer your question, so you can feel free to ignore
all of this :D but...

On Fri, 2004-02-06 at 05:27, Todd O'Bryan wrote:
> I've set up a single servlet to handle all requests for my webapp,
> hopefully avoiding massive changes to web.xml.

So instead you'll make massive changes to your servlet source?

>public Template handleRequest(HttpServletRequest request,
>                              HttpServletResponse response,
>                              Context ctx) throws Exception {
>    String path = request.getPathInfo();
>    if (path.equals("login.html")) {
>        return getTemplate("templates/login.vm");
>    } else if (path.equals("menu.html")){
>        //would like to call the handleRequest method in a MenuServlet
class here
>    } else {
>        return getTemplate("templates/error.vm");
>    }
>}

(Note: tabs are evil :)

I'm not necessarily saying that you should use struts or another framework,
but having to change a servlet if you add/delete a page, or want to test
with a different version of a page, etc. seems rather cumbersome and a lot
more error prone than changing a config file or some sort!

Sorry, it's a crabby, dreary Friday morning and I'm off to a soul-sucking
consult ;)

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: Forwarding a getTemplate request

Posted by Dave Newton <da...@solaraccess.com>.
Unfortunately I can't answer your question, so you can feel free to
ignore all of this :D but...

On Fri, 2004-02-06 at 05:27, Todd O'Bryan wrote:
> I've set up a single servlet to handle all requests for my webapp, 
> hopefully avoiding massive changes to web.xml.

So instead you'll make massive changes to your servlet source?

>public Template handleRequest(HttpServletRequest request,
>                              HttpServletResponse response,
>                              Context ctx) throws Exception {
>    String path = request.getPathInfo();
>    if (path.equals("login.html")) {
>        return getTemplate("templates/login.vm");
>    } else if (path.equals("menu.html")){
>        //would like to call the handleRequest method in a MenuServlet class here
>    } else {
>        return getTemplate("templates/error.vm");
>    }
>}

(Note: tabs are evil :)

I'm not necessarily saying that you should use struts or another
framework, but having to change a servlet if you add/delete a page, or
want to test with a different version of a page, etc. seems rather
cumbersome and a lot more error prone than changing a config file or
some sort!

Sorry, it's a crabby, dreary Friday morning and I'm off to a
soul-sucking consult ;)

Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org