You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ivo Pletikosic <Iv...@Benetech.org> on 2004/11/24 01:49:24 UTC
Serve images thru VelocityServlet
Is it possible to serve images directly using a VelocityServlet? or have
a VelocityServlet not parse a file and return it as-is?
I have images sitting on my server that should not be reached thru http
directly. So I am looking to be able to serve them dynamically, so that
the tag
<img src='/image?id=10'/>
makes the servlet to look up the referenced image and return it to the
client.
Is this possible? I am overriding handleRequest() in a VelocityServlet,
but if I try to return a binary image file I get a ParseErrorException .
What I am trying to avoid is having a second Servlet that is just for
serving images.
Thanks,
Ivo
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Serve images thru VelocityServlet
Posted by Ross Nicoll <xu...@gmail.com>.
If you need to send binary files, couldn't you just do it through
HttpServlet? It should be possible to use both VelocityServlet and
HttpServlet in the same application, right?
On Tue, 23 Nov 2004 19:12:11 -0800, Will Glass-Husain <wg...@forio.com> wrote:
> Hi Ivo,
>
> My web application serves a mix of Velocity templates and binary files (e.g.
> images). You can parse the request URI, determine if it's a Velocity
> template or a file of another type (e.g. an image). If it's not a Velocity
> file, open a file stream and write it directly to the response.
>
> What I found is that this approach was significantly less efficient than
> having my servlet container send the file. I looked in the Apache Tomcat
> code, and discovered that the HTTP headers for caching are fairly complex.
> I'd implemented some but not all of them. The result was that I was sending
> the full image in almost every case to the browser. But if your web server
> follows the right protocols, the browser and the server can communicate and
> determine the image is cached and not required (significantly increasing
> response time).
>
> There's two different methods you can use to have the container handle the
> images. The best way is for your web.xml file to only map files with a
> certain extension (e.g. *.vm) to Velocity. You can also use a
> requestDispatcher to forward the request directly to your servlet containers
> file handler. The exact name of the servlet will vary from container to
> container. In Tomcat it's "default". In Bea WebLogic it's "FileServlet".
> But typically you don't need to know this; just map the Velocity files to
> the Velocity servlet and the container will handle the rest.
>
> WILL
>
>
>
>
> ----- Original Message -----
> From: "Shinobu Kawai" <sh...@gmail.com>
> To: "Velocity Users List" <ve...@jakarta.apache.org>
> Sent: Tuesday, November 23, 2004 5:31 PM
> Subject: Re: Serve images thru VelocityServlet
>
> >
> > Hi Ivo,
> >
> >> Is it possible to serve images directly using a VelocityServlet? or have
> >> a VelocityServlet not parse a file and return it as-is?
> >>
> >> I have images sitting on my server that should not be reached thru http
> >> directly. So I am looking to be able to serve them dynamically, so that
> >> the tag
> >>
> >> <img src='/image?id=10'/>
> >>
> >> makes the servlet to look up the referenced image and return it to the
> >> client.
> >>
> >> Is this possible? I am overriding handleRequest() in a VelocityServlet,
> >> but if I try to return a binary image file I get a ParseErrorException .
> >
> > Try overriding #doRequest() or making a filter.
> >
> > http://jakarta.apache.org/velocity/api/org/apache/velocity/servlet/VelocityServlet.html#doRequest(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)
> > http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/Filter.html
> >
> > ## Or, you can use a non-binary image format. B)
> >
> > I also suggest migrating to VelocityViewServlet.
> > http://jakarta.apache.org/velocity/tools/view/
> >
> >> What I am trying to avoid is having a second Servlet that is just for
> >> serving images.
> >
> > So you only have velocity templates and images in your web application,
> > is that right? If you have any other static contents, images are
> > nothing different than them. ;)
> > #*
> > But I don't see why you wouldn't want to use the default static content
> > handling servlet. (Maybe your servlet container doesn't have one?)
> > *#
> >
> > Best regards,
> > -- Shinobu Kawai
> >
> > --
> > Shinobu Kawai <sh...@gmail.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
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Serve images thru VelocityServlet
Posted by Will Glass-Husain <wg...@forio.com>.
Hi Ivo,
My web application serves a mix of Velocity templates and binary files (e.g.
images). You can parse the request URI, determine if it's a Velocity
template or a file of another type (e.g. an image). If it's not a Velocity
file, open a file stream and write it directly to the response.
What I found is that this approach was significantly less efficient than
having my servlet container send the file. I looked in the Apache Tomcat
code, and discovered that the HTTP headers for caching are fairly complex.
I'd implemented some but not all of them. The result was that I was sending
the full image in almost every case to the browser. But if your web server
follows the right protocols, the browser and the server can communicate and
determine the image is cached and not required (significantly increasing
response time).
There's two different methods you can use to have the container handle the
images. The best way is for your web.xml file to only map files with a
certain extension (e.g. *.vm) to Velocity. You can also use a
requestDispatcher to forward the request directly to your servlet containers
file handler. The exact name of the servlet will vary from container to
container. In Tomcat it's "default". In Bea WebLogic it's "FileServlet".
But typically you don't need to know this; just map the Velocity files to
the Velocity servlet and the container will handle the rest.
WILL
----- Original Message -----
From: "Shinobu Kawai" <sh...@gmail.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Tuesday, November 23, 2004 5:31 PM
Subject: Re: Serve images thru VelocityServlet
>
> Hi Ivo,
>
>> Is it possible to serve images directly using a VelocityServlet? or have
>> a VelocityServlet not parse a file and return it as-is?
>>
>> I have images sitting on my server that should not be reached thru http
>> directly. So I am looking to be able to serve them dynamically, so that
>> the tag
>>
>> <img src='/image?id=10'/>
>>
>> makes the servlet to look up the referenced image and return it to the
>> client.
>>
>> Is this possible? I am overriding handleRequest() in a VelocityServlet,
>> but if I try to return a binary image file I get a ParseErrorException .
>
> Try overriding #doRequest() or making a filter.
>
> http://jakarta.apache.org/velocity/api/org/apache/velocity/servlet/VelocityServlet.html#doRequest(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)
> http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/Filter.html
>
> ## Or, you can use a non-binary image format. B)
>
> I also suggest migrating to VelocityViewServlet.
> http://jakarta.apache.org/velocity/tools/view/
>
>> What I am trying to avoid is having a second Servlet that is just for
>> serving images.
>
> So you only have velocity templates and images in your web application,
> is that right? If you have any other static contents, images are
> nothing different than them. ;)
> #*
> But I don't see why you wouldn't want to use the default static content
> handling servlet. (Maybe your servlet container doesn't have one?)
> *#
>
> Best regards,
> -- Shinobu Kawai
>
> --
> Shinobu Kawai <sh...@gmail.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: Serve images thru VelocityServlet
Posted by Shinobu Kawai <sh...@gmail.com>.
Hi Ivo,
> Is it possible to serve images directly using a VelocityServlet? or have
> a VelocityServlet not parse a file and return it as-is?
>
> I have images sitting on my server that should not be reached thru http
> directly. So I am looking to be able to serve them dynamically, so that
> the tag
>
> <img src='/image?id=10'/>
>
> makes the servlet to look up the referenced image and return it to the
> client.
>
> Is this possible? I am overriding handleRequest() in a VelocityServlet,
> but if I try to return a binary image file I get a ParseErrorException .
Try overriding #doRequest() or making a filter.
http://jakarta.apache.org/velocity/api/org/apache/velocity/servlet/VelocityServlet.html#doRequest(javax.servlet.http.HttpServletRequest,%20javax.servlet.http.HttpServletResponse)
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/Filter.html
## Or, you can use a non-binary image format. B)
I also suggest migrating to VelocityViewServlet.
http://jakarta.apache.org/velocity/tools/view/
> What I am trying to avoid is having a second Servlet that is just for
> serving images.
So you only have velocity templates and images in your web application,
is that right? If you have any other static contents, images are
nothing different than them. ;)
#*
But I don't see why you wouldn't want to use the default static content
handling servlet. (Maybe your servlet container doesn't have one?)
*#
Best regards,
-- Shinobu Kawai
--
Shinobu Kawai <sh...@gmail.com>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org