You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by David Donohue <dd...@daviddonohue.com> on 2007/04/15 06:31:17 UTC

ClasspathResourceLoader trouble loading images, css

I am enjoying using VelocityLayoutServlet with ClasspathResourceLoader.  VM
files load great, and the layout loads wonderfully too. The trick is: how
can I get images, CSS, and other static content to load via classpath as
well?  Or how can I get them load at all?
THANKS!
David

Re: ClasspathResourceLoader trouble loading images, css

Posted by Will Glass-Husain <wg...@gmail.com>.
Basically what I was thinking, though I didn't know you could get the
container MIME type via the call to ServletContext.

You'll find that your webapps request every image every time though (no
browser caching) since you are not sending the various caching headers.

WILL




On 4/15/07, David Donohue <dd...@daviddonohue.com> wrote:
>
> I found a solution.  I subclassed VelocitylayoutServlet and overrode
> doRequest method as follows.  I am assuming you have set up your Java web
> server (e.g. Tomcat) to send all content thru this servlet.  This servlet
> then farms requests to *.vm to Velocity layout infrastructure, and for all
> other requests, it streams binary content from classpath tot the browser.
>
> public class MyVelocityLayoutServlet extends VelocityLayoutServlet {
>
> /**
>      * Override doRequest, to handle static content
>      */
>     protected void doRequest(HttpServletRequest request,
> HttpServletResponse
> response) throws ServletException, IOException {
>         String path = ServletUtils.getPath(request);
>         String mt=getServletContext().getMimeType(path);
>       //if (mt!=null) response.setContentType(mt);
>         String extension = "vm";
>         if (path.indexOf(".") >= 0) {
>             String[] pathArr = path.split("\\.");
>             extension = pathArr[pathArr.length - 1].toLowerCase();
>         }
>
>         if (extension.equals("vm")) {
>             log.trace("Sending to VelocityLayoutServlet: Path=" + path +
> ";
> MIME-type=" + mt + "; extension=" + extension);
>             super.doRequest(request, response);
>         } else {
>             log.trace("Streaming to browser: Path=" + path + ";
> MIME-type="
> + mt + "; extension=" + extension);
>             response.setContentType(mt);
>             InputStream in = this.getClass().getResourceAsStream (path);
>             OutputStream out = response.getOutputStream();
>             int b;
>             try {
>                 while ((b = in.read()) != -1) {
>                     out.write(b);
>                 }
>             } finally {
>         if (in != null) {
>             in.close();
>         }
>         if (out != null) {
>             out.close();
>         }
>             }
>         }
>     }
>
> ---------- Forwarded message ----------
> From: David Donohue <dd...@daviddonohue.com>
> Date: Apr 15, 2007 12:31 AM
> Subject: ClasspathResourceLoader trouble loading images, css
> To: Velocity Users List <us...@velocity.apache.org>
>
> I am enjoying using VelocityLayoutServlet with
> ClasspathResourceLoader.  VM
> files load great, and the layout loads wonderfully too. The trick is: how
> can I get images, CSS, and other static content to load via classpath as
> well?  Or how can I get them load at all?
> THANKS!
> David
>



-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Fwd: ClasspathResourceLoader trouble loading images, css

Posted by David Donohue <dd...@daviddonohue.com>.
I found a solution.  I subclassed VelocitylayoutServlet and overrode
doRequest method as follows.  I am assuming you have set up your Java web
server (e.g. Tomcat) to send all content thru this servlet.  This servlet
then farms requests to *.vm to Velocity layout infrastructure, and for all
other requests, it streams binary content from classpath tot the browser.

public class MyVelocityLayoutServlet extends VelocityLayoutServlet {

/**
     * Override doRequest, to handle static content
     */
    protected void doRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
        String path = ServletUtils.getPath(request);
        String mt=getServletContext().getMimeType(path);
      //if (mt!=null) response.setContentType(mt);
        String extension = "vm";
        if (path.indexOf(".") >= 0) {
            String[] pathArr = path.split("\\.");
            extension = pathArr[pathArr.length - 1].toLowerCase();
        }

        if (extension.equals("vm")) {
            log.trace("Sending to VelocityLayoutServlet: Path=" + path + ";
MIME-type=" + mt + "; extension=" + extension);
            super.doRequest(request, response);
        } else {
            log.trace("Streaming to browser: Path=" + path + "; MIME-type="
+ mt + "; extension=" + extension);
            response.setContentType(mt);
            InputStream in = this.getClass().getResourceAsStream (path);
            OutputStream out = response.getOutputStream();
            int b;
            try {
                while ((b = in.read()) != -1) {
                    out.write(b);
                }
            } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
            }
        }
    }

---------- Forwarded message ----------
From: David Donohue <dd...@daviddonohue.com>
Date: Apr 15, 2007 12:31 AM
Subject: ClasspathResourceLoader trouble loading images, css
To: Velocity Users List <us...@velocity.apache.org>

I am enjoying using VelocityLayoutServlet with ClasspathResourceLoader.  VM
files load great, and the layout loads wonderfully too. The trick is: how
can I get images, CSS, and other static content to load via classpath as
well?  Or how can I get them load at all?
THANKS!
David

Re: ClasspathResourceLoader trouble loading images, css

Posted by David Donohue <dd...@daviddonohue.com>.
Agreed that my solution does nto attempt to cache anything and will suffer
performance issues.  But I am using Jetty 6.1.1 and you need to add your own
static content loader.  I could not figure out how to configure Jetty to use
its DefaultServlet to load content from classpath.

On 4/15/07, Will Glass-Husain <wg...@gmail.com> wrote:
>
> Although -- quick note from practical experience-- it's much better to
> have the container (Apache or Tomcat) send static files whenever possible.
> In particular, to implement browser caching there's a host of HTTP headers
> that have to be set properly.  Without them your web app will be
> significantly slower.
>
> WILL
>
> On 4/15/07, Will Glass-Husain <wg...@gmail.com> wrote:
> >
> > Well, Velocity can load any text files, but there's a risk it might
> > process random characters as FML.  (e.g. "##" as a comment).  And binary
> > files such as images are problematic.
> >
> > Maybe use ClassLoader.getSystemResourceAsStream () instead?
> >
> > WILL
> >
> > On 4/14/07, David Donohue < dd@daviddonohue.com> wrote:
> > >
> > > I am enjoying using VelocityLayoutServlet with
> > > ClasspathResourceLoader.  VM
> > > files load great, and the layout loads wonderfully too. The trick is:
> > > how
> > > can I get images, CSS, and other static content to load via classpath
> > > as
> > > well?  Or how can I get them load at all?
> > > THANKS!
> > > David
> > >
> >
> >
> >
> > --
> > Forio Business Simulations
> >
> > Will Glass-Husain
> > wglass@forio.com
> > www.forio.com
>
>
>
>
> --
> Forio Business Simulations
>
> Will Glass-Husain
> wglass@forio.com
> www.forio.com
>

Re: ClasspathResourceLoader trouble loading images, css

Posted by Will Glass-Husain <wg...@gmail.com>.
Although -- quick note from practical experience-- it's much better to have
the container (Apache or Tomcat) send static files whenever possible.  In
particular, to implement browser caching there's a host of HTTP headers that
have to be set properly.  Without them your web app will be significantly
slower.

WILL

On 4/15/07, Will Glass-Husain <wg...@gmail.com> wrote:
>
> Well, Velocity can load any text files, but there's a risk it might
> process random characters as FML.  (e.g. "##" as a comment).  And binary
> files such as images are problematic.
>
> Maybe use ClassLoader.getSystemResourceAsStream () instead?
>
> WILL
>
> On 4/14/07, David Donohue <dd...@daviddonohue.com> wrote:
> >
> > I am enjoying using VelocityLayoutServlet with
> > ClasspathResourceLoader.  VM
> > files load great, and the layout loads wonderfully too. The trick is:
> > how
> > can I get images, CSS, and other static content to load via classpath as
> >
> > well?  Or how can I get them load at all?
> > THANKS!
> > David
> >
>
>
>
> --
> Forio Business Simulations
>
> Will Glass-Husain
> wglass@forio.com
> www.forio.com




-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com

Re: ClasspathResourceLoader trouble loading images, css

Posted by Will Glass-Husain <wg...@gmail.com>.
Well, Velocity can load any text files, but there's a risk it might process
random characters as FML.  (e.g. "##" as a comment).  And binary files such
as images are problematic.

Maybe use ClassLoader.getSystemResourceAsStream() instead?

WILL

On 4/14/07, David Donohue <dd...@daviddonohue.com> wrote:
>
> I am enjoying using VelocityLayoutServlet with
> ClasspathResourceLoader.  VM
> files load great, and the layout loads wonderfully too. The trick is: how
> can I get images, CSS, and other static content to load via classpath as
> well?  Or how can I get them load at all?
> THANKS!
> David
>



-- 
Forio Business Simulations

Will Glass-Husain
wglass@forio.com
www.forio.com