You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Søren Neigaard <ne...@e-box.dk> on 2002/11/26 20:36:53 UTC

Newbie - Keep getting ResourceNotFoundException

I'm trying to make a simple HelloWorld example, and I'm doing this
from inside the doGet method in a servlet:

------------------
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("name", new String("Velocity Test"));
Template template = null;
try {
  template = Velocity.getTemplate("mytemplate.vm");
  template.merge(context,response.getWriter());
}
catch(Exception e){
  e.printStackTrace();
}
------------------

Where does it look for the template? Can it be on the file system, or
must it be accessible via HTTP? I have now tried to pint it directly
to the path where the file is, and i get this error:

------------------
org.apache.velocity.exception.ResourceNotFoundException: Unable to
find resource
'/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
------------------

And the file is there!? Please advice, what is my (simple) problem?

--
Med venlig hilsen/Best regards,
 Søren Neigaard mailto:neigaard@e-box.dk
--
 "First we thought the PC was a calculator. Then we found out how to turn numbers into letters with ASCII — and we thought it was a typewriter. Then we discovered graphics, and we thought it was a television. With the World Wide Web, we've realized it's a brochure."


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie - Keep getting ResourceNotFoundException

Posted by Antonie C Malan <ma...@optusnet.com.au>.
Hi Soren,

Sorry, my keyboard doesn't do the / and o as one as used in Scandinavia.

I had the same problem.  Velocity looks for the vm file in the same directory 
it tries to write the velocity.log to.  In my case it was $TOMCAT_HOME/bin.
Crazy, isn't it?

I found the cure in an example.  Add this to your  code below:
	 /**
     *   Called by the VelocityServlet
     *   init().  We want to set a set of properties
     *   so that templates will be found in the webapp
     *   root.  This makes this easier to work with as 
     *   an example, so a new user doesn't have to worry
     *   about config issues when first figuring things
     *   out
     */
    protected Properties loadConfiguration(ServletConfig config )
        throws IOException, FileNotFoundException
    {
        Properties props = new Properties();

        /*
         *  first, we set the template path for the
         *  FileResourceLoader to the root of your
         *  web application.  This should work under tomcat   
         */

        String path = config.getServletContext().getRealPath("/");

        if (path == null)
        {
            System.out.println(" SampleServlet.loadConfiguration() : unable to 
" 
                               + "get the current webapp root.  Using '/'. 
Please fix.");

            path = "/";
        }

        props.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH,  path );

        /**
         *  and the same for the log file
         */

        props.setProperty( "runtime.log", path + "velocity.log" );

        return props;
    }

It worked and velocity.log is then written to the root directory of your 
specific web application.  That's where your vm file should also be.

Hope it helps.

On Wed 27 Nov 02 6:36, Søren Neigaard wrote:
> I'm trying to make a simple HelloWorld example, and I'm doing this
> from inside the doGet method in a servlet:
>
> ------------------
> Velocity.init();
> VelocityContext context = new VelocityContext();
> context.put("name", new String("Velocity Test"));
> Template template = null;
> try {
>   template = Velocity.getTemplate("mytemplate.vm");
>   template.merge(context,response.getWriter());
> }
> catch(Exception e){
>   e.printStackTrace();
> }
> ------------------
>
> Where does it look for the template? Can it be on the file system, or
> must it be accessible via HTTP? I have now tried to pint it directly
> to the path where the file is, and i get this error:

Chris Malan
malan2000@optusnet.com.au

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[2]: Newbie - Keep getting ResourceNotFoundException

Posted by Søren Neigaard <ne...@e-box.dk>.
Hmm well I can see that I will have to figure this out myself,
alright then :)

/Søren

Tuesday, November 26, 2002, 9:11:16 PM, Matthew wrote:

MW> I would advise first working with Velocity by extending VelocityServlet. 
MW> Once you've used it a bit, you can then jump into more challenging tasks 
MW> like the one you have mentioned.

MW> -Matt

MW> Søren Neigaard wrote:

>>Well I have searched the archives, and found a lot, but nothing that
>>helped me :( I want total controll over my servlet, so I rather not
>>use the VelocityServlet. I don't understand why my Servlet has
>>problems finding the template, since I give it the correct path to it,
>>I even tried to give it the HTTP path like this
>>"/myproject/mytemplate.yaf" (and if I requested this path manually
>>with my browser, I would get the template file). Could you maybe
>>explain to me what can possible be wrong, do I need to initialize
>>something, or what is it?
>>
>>/Søren
>>
>>Tuesday, November 26, 2002, 8:54:54 PM, Jeff wrote:
>>
>>JD> You servlet is having a problem finding the template. If you search the archives you find a bunch of articles that explain how to fix this.
>>JD> Instead I think it is easier to get started with Velocity in Servlets by using VelocityServlet and extending it. This does most of the initalization stuff.
>>
>>JD> HTH,
>>
>>JD> Jeff
>>
>>JD> Søren Neigaard wrote:
>>
>>  
>>
>>>>I'm trying to make a simple HelloWorld example, and I'm doing this
>>>>from inside the doGet method in a servlet:
>>>>
>>>>------------------
>>>>Velocity.init();
>>>>VelocityContext context = new VelocityContext();
>>>>context.put("name", new String("Velocity Test"));
>>>>Template template = null;
>>>>try {
>>>>  template = Velocity.getTemplate("mytemplate.vm");
>>>>  template.merge(context,response.getWriter());
>>>>}
>>>>catch(Exception e){
>>>>  e.printStackTrace();
>>>>}
>>>>------------------
>>>>
>>>>Where does it look for the template? Can it be on the file system, or
>>>>must it be accessible via HTTP? I have now tried to pint it directly
>>>>to the path where the file is, and i get this error:
>>>>
>>>>------------------
>>>>org.apache.velocity.exception.ResourceNotFoundException: Unable to
>>>>find resource
>>>>'/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
>>>>------------------
>>>>
>>>>And the file is there!? Please advice, what is my (simple) problem?
>>>>
>>>>--
>>>>Med venlig hilsen/Best regards,
>>>> Søren Neigaard mailto:neigaard@e-box.dk


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie - Keep getting ResourceNotFoundException

Posted by Matthew Walker <mw...@electronicdatainc.com>.
I would advise first working with Velocity by extending VelocityServlet. 
Once you've used it a bit, you can then jump into more challenging tasks 
like the one you have mentioned.

-Matt

Søren Neigaard wrote:

>Well I have searched the archives, and found a lot, but nothing that
>helped me :( I want total controll over my servlet, so I rather not
>use the VelocityServlet. I don't understand why my Servlet has
>problems finding the template, since I give it the correct path to it,
>I even tried to give it the HTTP path like this
>"/myproject/mytemplate.yaf" (and if I requested this path manually
>with my browser, I would get the template file). Could you maybe
>explain to me what can possible be wrong, do I need to initialize
>something, or what is it?
>
>/Søren
>
>Tuesday, November 26, 2002, 8:54:54 PM, Jeff wrote:
>
>JD> You servlet is having a problem finding the template. If you search the archives you find a bunch of articles that explain how to fix this.
>JD> Instead I think it is easier to get started with Velocity in Servlets by using VelocityServlet and extending it. This does most of the initalization stuff.
>
>JD> HTH,
>
>JD> Jeff
>
>JD> Søren Neigaard wrote:
>
>  
>
>>>I'm trying to make a simple HelloWorld example, and I'm doing this
>>>from inside the doGet method in a servlet:
>>>
>>>------------------
>>>Velocity.init();
>>>VelocityContext context = new VelocityContext();
>>>context.put("name", new String("Velocity Test"));
>>>Template template = null;
>>>try {
>>>  template = Velocity.getTemplate("mytemplate.vm");
>>>  template.merge(context,response.getWriter());
>>>}
>>>catch(Exception e){
>>>  e.printStackTrace();
>>>}
>>>------------------
>>>
>>>Where does it look for the template? Can it be on the file system, or
>>>must it be accessible via HTTP? I have now tried to pint it directly
>>>to the path where the file is, and i get this error:
>>>
>>>------------------
>>>org.apache.velocity.exception.ResourceNotFoundException: Unable to
>>>find resource
>>>'/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
>>>------------------
>>>
>>>And the file is there!? Please advice, what is my (simple) problem?
>>>
>>>--
>>>Med venlig hilsen/Best regards,
>>> Søren Neigaard mailto:neigaard@e-box.dk
>>>      
>>>
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>  
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[2]: Newbie - Keep getting ResourceNotFoundException

Posted by Søren Neigaard <ne...@e-box.dk>.
Well I have searched the archives, and found a lot, but nothing that
helped me :( I want total controll over my servlet, so I rather not
use the VelocityServlet. I don't understand why my Servlet has
problems finding the template, since I give it the correct path to it,
I even tried to give it the HTTP path like this
"/myproject/mytemplate.yaf" (and if I requested this path manually
with my browser, I would get the template file). Could you maybe
explain to me what can possible be wrong, do I need to initialize
something, or what is it?

/Søren

Tuesday, November 26, 2002, 8:54:54 PM, Jeff wrote:

JD> You servlet is having a problem finding the template. If you search the archives you find a bunch of articles that explain how to fix this.
JD> Instead I think it is easier to get started with Velocity in Servlets by using VelocityServlet and extending it. This does most of the initalization stuff.

JD> HTH,

JD> Jeff

JD> Søren Neigaard wrote:

>> I'm trying to make a simple HelloWorld example, and I'm doing this
>> from inside the doGet method in a servlet:
>>
>> ------------------
>> Velocity.init();
>> VelocityContext context = new VelocityContext();
>> context.put("name", new String("Velocity Test"));
>> Template template = null;
>> try {
>>   template = Velocity.getTemplate("mytemplate.vm");
>>   template.merge(context,response.getWriter());
>> }
>> catch(Exception e){
>>   e.printStackTrace();
>> }
>> ------------------
>>
>> Where does it look for the template? Can it be on the file system, or
>> must it be accessible via HTTP? I have now tried to pint it directly
>> to the path where the file is, and i get this error:
>>
>> ------------------
>> org.apache.velocity.exception.ResourceNotFoundException: Unable to
>> find resource
>> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
>> ------------------
>>
>> And the file is there!? Please advice, what is my (simple) problem?
>>
>> --
>> Med venlig hilsen/Best regards,
>>  Søren Neigaard mailto:neigaard@e-box.dk


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie - Keep getting ResourceNotFoundException

Posted by Jeff Duska <Je...@noaa.gov>.
You servlet is having a problem finding the template. If you search the archives you find a bunch of articles that explain how to fix this.
Instead I think it is easier to get started with Velocity in Servlets by using VelocityServlet and extending it. This does most of the initalization stuff.

HTH,

Jeff

Søren Neigaard wrote:

> I'm trying to make a simple HelloWorld example, and I'm doing this
> from inside the doGet method in a servlet:
>
> ------------------
> Velocity.init();
> VelocityContext context = new VelocityContext();
> context.put("name", new String("Velocity Test"));
> Template template = null;
> try {
>   template = Velocity.getTemplate("mytemplate.vm");
>   template.merge(context,response.getWriter());
> }
> catch(Exception e){
>   e.printStackTrace();
> }
> ------------------
>
> Where does it look for the template? Can it be on the file system, or
> must it be accessible via HTTP? I have now tried to pint it directly
> to the path where the file is, and i get this error:
>
> ------------------
> org.apache.velocity.exception.ResourceNotFoundException: Unable to
> find resource
> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
> ------------------
>
> And the file is there!? Please advice, what is my (simple) problem?
>
> --
> Med venlig hilsen/Best regards,
>  Søren Neigaard mailto:neigaard@e-box.dk
> --
>  "First we thought the PC was a calculator. Then we found out how to turn numbers into letters with ASCII — and we thought it was a typewriter. Then we discovered graphics, and we thought it was a television. With the World Wide Web, we've realized it's a brochure."
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Re[2]: Newbie - Keep getting ResourceNotFoundException

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
On Wednesday, November 27, 2002, at 02:21 PM, Søren Neigaard wrote:

> Yes I figured it out myself, just had to read some more. Although I
> have been in the computer business for quite some time now, I still
> have a tendency to ask firs, and look last - sorry I know it's a bad
> habit :) Thanks for the answer though.

I do the same thing, btw.  It's a terrible habit. :)

>
> /Søren
>
> Wednesday, November 27, 2002, 6:17:47 PM, Geir wrote:
>
> GMJ> If you already solve the problem, ignore - my scan of the list 
> didn't
> GMJ> seem to have any help for you...
>
> GMJ> On Tuesday, November 26, 2002, at 02:36 PM, Søren Neigaard wrote:
>
>>> I'm trying to make a simple HelloWorld example, and I'm doing this
>>> from inside the doGet method in a servlet:
>>>
>>> ------------------
>>> Velocity.init();
>>> VelocityContext context = new VelocityContext();
>>> context.put("name", new String("Velocity Test"));
>>> Template template = null;
>>> try {
>>>   template = Velocity.getTemplate("mytemplate.vm");
>>>   template.merge(context,response.getWriter());
>>> }
>>> catch(Exception e){
>>>   e.printStackTrace();
>>> }
>>> ------------------
>>>
>>> Where does it look for the template? Can it be on the file system, or
>>> must it be accessible via HTTP? I have now tried to pint it directly
>>> to the path where the file is, and i get this error:
>>>
>>> ------------------
>>> org.apache.velocity.exception.ResourceNotFoundException: Unable to
>>> find resource
>>> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
>>> ------------------
>>>
>>> And the file is there!? Please advice, what is my (simple) problem?
>
> GMJ> It's a simple problem.
>
> GMJ> First, some comments :
>
> GMJ> 1) you don't want to put init() in the doGet().  First, it's not 
> going
> GMJ> to work after the first time, and two, it's much better to put 
> this
> GMJ> into the servlets' init method, so it will get called at the 
> right time
> GMJ> in the servlet life cycle.
>
> GMJ> 2) There is nothing wrong with rolling your own servlet - it's 
> what I
> GMJ> do and can customize things the way I want them to be.  
> VelocityServlet
> GMJ> is a nice base, and it has a nice model to work with, but there 
> are
> GMJ> many ways to do it.
>
> GMJ> 3) The problem - the problem is that by default, velocity's 
> resource
> GMJ> loader is the FileResourceLoader, and further, it defaults to the
> GMJ> 'current directory', whatever that is, when init()-ed w/o 
> parameters.
> GMJ> The solution is really simple - you want to set the file resource
> GMJ> loader path with the right thing - see how VelocityServlet does 
> it, or
> GMJ> the servlet examples in the distro.
>
> GMJ> Other solutions is to use the webapp resource loader from the 
> tools
> GMJ> project as well, as that is meant to not depend on the filesystem,
> GMJ> which is the better thing to do in a servlet environment.
>
> GMJ> geir
>
>
> --
> To unsubscribe, e-mail:   
> <ma...@jakarta.apache.org>
> For additional commands, e-mail: 
> <ma...@jakarta.apache.org>
>
>
-- 
Geir Magnusson Jr                                   203-355-2219(w)
Adeptra, Inc.                                       203-247-1713(m)
geirm@adeptra.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re[2]: Newbie - Keep getting ResourceNotFoundException

Posted by Søren Neigaard <ne...@e-box.dk>.
Yes I figured it out myself, just had to read some more. Although I
have been in the computer business for quite some time now, I still
have a tendency to ask firs, and look last - sorry I know it's a bad
habit :) Thanks for the answer though.

/Søren

Wednesday, November 27, 2002, 6:17:47 PM, Geir wrote:

GMJ> If you already solve the problem, ignore - my scan of the list didn't 
GMJ> seem to have any help for you...

GMJ> On Tuesday, November 26, 2002, at 02:36 PM, Søren Neigaard wrote:

>> I'm trying to make a simple HelloWorld example, and I'm doing this
>> from inside the doGet method in a servlet:
>>
>> ------------------
>> Velocity.init();
>> VelocityContext context = new VelocityContext();
>> context.put("name", new String("Velocity Test"));
>> Template template = null;
>> try {
>>   template = Velocity.getTemplate("mytemplate.vm");
>>   template.merge(context,response.getWriter());
>> }
>> catch(Exception e){
>>   e.printStackTrace();
>> }
>> ------------------
>>
>> Where does it look for the template? Can it be on the file system, or
>> must it be accessible via HTTP? I have now tried to pint it directly
>> to the path where the file is, and i get this error:
>>
>> ------------------
>> org.apache.velocity.exception.ResourceNotFoundException: Unable to
>> find resource
>> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
>> ------------------
>>
>> And the file is there!? Please advice, what is my (simple) problem?

GMJ> It's a simple problem.

GMJ> First, some comments :

GMJ> 1) you don't want to put init() in the doGet().  First, it's not going 
GMJ> to work after the first time, and two, it's much better to put this 
GMJ> into the servlets' init method, so it will get called at the right time 
GMJ> in the servlet life cycle.

GMJ> 2) There is nothing wrong with rolling your own servlet - it's what I 
GMJ> do and can customize things the way I want them to be.  VelocityServlet 
GMJ> is a nice base, and it has a nice model to work with, but there are 
GMJ> many ways to do it.

GMJ> 3) The problem - the problem is that by default, velocity's resource 
GMJ> loader is the FileResourceLoader, and further, it defaults to the 
GMJ> 'current directory', whatever that is, when init()-ed w/o parameters.  
GMJ> The solution is really simple - you want to set the file resource 
GMJ> loader path with the right thing - see how VelocityServlet does it, or 
GMJ> the servlet examples in the distro.

GMJ> Other solutions is to use the webapp resource loader from the tools 
GMJ> project as well, as that is meant to not depend on the filesystem, 
GMJ> which is the better thing to do in a servlet environment.

GMJ> geir


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Newbie - Keep getting ResourceNotFoundException

Posted by "Geir Magnusson Jr." <ge...@adeptra.com>.
If you already solve the problem, ignore - my scan of the list didn't 
seem to have any help for you...

On Tuesday, November 26, 2002, at 02:36 PM, Søren Neigaard wrote:

> I'm trying to make a simple HelloWorld example, and I'm doing this
> from inside the doGet method in a servlet:
>
> ------------------
> Velocity.init();
> VelocityContext context = new VelocityContext();
> context.put("name", new String("Velocity Test"));
> Template template = null;
> try {
>   template = Velocity.getTemplate("mytemplate.vm");
>   template.merge(context,response.getWriter());
> }
> catch(Exception e){
>   e.printStackTrace();
> }
> ------------------
>
> Where does it look for the template? Can it be on the file system, or
> must it be accessible via HTTP? I have now tried to pint it directly
> to the path where the file is, and i get this error:
>
> ------------------
> org.apache.velocity.exception.ResourceNotFoundException: Unable to
> find resource
> '/C:/jakarta-tomcat-4.1.10-LE-jdk14/webapps/yaf/mytemplate.vm'
> ------------------
>
> And the file is there!? Please advice, what is my (simple) problem?

It's a simple problem.

First, some comments :

1) you don't want to put init() in the doGet().  First, it's not going 
to work after the first time, and two, it's much better to put this 
into the servlets' init method, so it will get called at the right time 
in the servlet life cycle.

2) There is nothing wrong with rolling your own servlet - it's what I 
do and can customize things the way I want them to be.  VelocityServlet 
is a nice base, and it has a nice model to work with, but there are 
many ways to do it.

3) The problem - the problem is that by default, velocity's resource 
loader is the FileResourceLoader, and further, it defaults to the 
'current directory', whatever that is, when init()-ed w/o parameters.  
The solution is really simple - you want to set the file resource 
loader path with the right thing - see how VelocityServlet does it, or 
the servlet examples in the distro.

Other solutions is to use the webapp resource loader from the tools 
project as well, as that is meant to not depend on the filesystem, 
which is the better thing to do in a servlet environment.

geir

-- 
Geir Magnusson Jr                                   203-355-2219(w)
Adeptra, Inc.                                       203-247-1713(m)
geirm@adeptra.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>