You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by jeff <jl...@houseofdistraction.com> on 2001/01/26 04:39:25 UTC

BUG: HttpServletRequest in a VelocityContext

There's a problem I've had using velocity under Tomcat for quite a while
now.  I've got a workaround for it but it still bugs me because I don't
understand why it should be.

I always take the HttpServletRequest object from the parameters to doGet
and doPost and put it into the VelocityContext.  Well, I would like to,
but it doesn't work.  e.g.

public void doGet( HttpServletRequest request, HttpServletResponse
response )
{
    VelocityContext vContext = new VelocityContext();
    vContext.put( "request", request );

    // ...

    template.merge( vContext, response.getWriter() );
}


-------------- template -----------------
$request<br>
$request.getMethod()<br>
-----------------------------------------

With the above code and template I get the following output

-------------- output -----------------
org.apache.tomcat.facade.HttpServletRequestFacade@322bce
$realRequest.getMethod()
---------------------------------------

My workaround all these past months has been a class called RequestProxy
which is a straight passthru of all the request methods:

public class RequestProxy
{
    HttpServletRequest request;

    public RequestProxy( HttpServletRequest request )
    {
        this.request = request;
    }

    // ...

    public String getMethod()
    {
        return request.getMethod();
    }
  
    // ...
}

So if I modify my code above and change request to this:

    vContext.put( "request", new RequestProxy( request ) );

I get this output (i.e. it works fine)
-------------- output -----------------
test.RequestProxy@67e3a5
GET
---------------------------------------


Here's my system details
NTsp6
JDK1.3
tomcat 3.2
velocity (recent CVS)

Obviously this isn't a very critical bug for me, but it might be a nasty
thing to explain to new users.  If anyone wants to pursue this, I would
be happy to package up the test case and send it to you.

Re: Charset in velocity templates

Posted by ??sta? Ste????? <cs...@aias.gr>.
Thanks, now it works fine
----- Original Message -----
From: "Geir Magnusson Jr." <ge...@optonline.net>
To: <ve...@jakarta.apache.org>
Sent: Friday, January 26, 2001 12:26 PM
Subject: Re: Charset in velocity templates


> ??sta? Ste????? wrote:
> >
> > Any ideas how can I set the ContentType of the output that Velocity
> > produces?
> >
> > I am trying to use a different charset than the 8859-1 but does not seem
to
> > work.
> >
> > Any ideas?
> > Is there any property I can set?
>
> If you are using the VelocityServlet base class, take a look to see how
> that is handled.
>
> The revelevant default properties are :
>
> template.encoding=8859_1
> default.contentType=text/html
>
> and are found in
> org/apache/velocity/runtime/defaults/velocity.properties in the source
> tree
>
> geir
>
> --
> Geir Magnusson Jr.                               geirm@optonline.com
> Velocity : it's not just a good idea. It should be the law.
> http://jakarta.apache.org/velocity


Re: Charset in velocity templates

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
??sta? Ste????? wrote:
> 
> Any ideas how can I set the ContentType of the output that Velocity
> produces?
> 
> I am trying to use a different charset than the 8859-1 but does not seem to
> work.
> 
> Any ideas?
> Is there any property I can set?

If you are using the VelocityServlet base class, take a look to see how
that is handled.

The revelevant default properties are :

template.encoding=8859_1
default.contentType=text/html

and are found in
org/apache/velocity/runtime/defaults/velocity.properties in the source
tree

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity

Charset in velocity templates

Posted by ??sta? Ste????? <cs...@aias.gr>.
Any ideas how can I set the ContentType of the output that Velocity
produces?

I am trying to use a different charset than the 8859-1 but does not seem to
work.

Any ideas?
Is there any property I can set?



Re: BUG: HttpServletRequest in a VelocityContext

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
jeff wrote:
> 
> There's a problem I've had using velocity under Tomcat for quite a while
> now.  I've got a workaround for it but it still bugs me because I don't
> understand why it should be.

Let us know as soon as you find these things.  This shouldn't require a
work around...

> I always take the HttpServletRequest object from the parameters to doGet
> and doPost and put it into the VelocityContext.  Well, I would like to,
> but it doesn't work.  e.g.
> 
> public void doGet( HttpServletRequest request, HttpServletResponse
> response )
> {
>     VelocityContext vContext = new VelocityContext();
>     vContext.put( "request", request );
> 
>     // ...
> 
>     template.merge( vContext, response.getWriter() );
> }
> 
> -------------- template -----------------
> $request<br>
> $request.getMethod()<br>
> -----------------------------------------
> 
> With the above code and template I get the following output
> 
> -------------- output -----------------
> org.apache.tomcat.facade.HttpServletRequestFacade@322bce
> $realRequest.getMethod()
> ---------------------------------------

This is really surprising, especially the bit with
'$request.getMethod()' getting rendered as '$realRequest.getMethod()'

I looked at o.a.t.f.HttpServletRequestFacade, and it's

public String getMethod()

so this is really odd...

I will give this a try tomorrow.


[SNIP]
> 
> Obviously this isn't a very critical bug for me, but it might be a nasty
> thing to explain to new users.  If anyone wants to pursue this, I would
> be happy to package up the test case and send it to you.

Yes, we will definitely hunt this down and kill it...

geir

-- 
Geir Magnusson Jr.                               geirm@optonline.com
Velocity : it's not just a good idea. It should be the law.
http://jakarta.apache.org/velocity