You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Matt Veitas <mv...@gmail.com> on 2010/03/11 15:38:01 UTC

Applying a servlet filter to a static resource

Hi,

I am using Tomcat 6.0.24 and am trying to use a servlet filter on a static
resource and am having some troubles as the 2nd request returns a 304. If I
could modify the calling code to hit a servlet instead of the static
resource, I could use that servlet to read the static resource (JSON),
perform some business logic and modify the JSON, and return that to the
client, but unfortunately I can't change the calling url.

<filter>
   <filter-name>TestFilter</filter-name>
   <filter-class>foo.TestFilter</filter-class>
</filter>
<filter-mapping>
   <filter-name>TestFilter</filter-name>
   <url-pattern>/js/config/my.json</url-pattern>
</filter-mapping>

Is there anything I can do to make Tomcat not return the 304 for the 2nd
request to my.json?

Re: Applying a servlet filter to a static resource

Posted by André Warnier <aw...@ice-sa.com>.
André Warnier wrote:
...

> I have not tested the following, and it is not based on any certain 
> knowledge based on the HTTP RFCs, so treat with caution, but you could 
> try a cheap trick :
> In the link to that "document" in the html page, add a dummy query 
> string, like :
> instead of
> http://servername/webappname/js/config/my.json
> try
> http://servername/webappname/js/config/my.json?a=my.json
> 
> The presence of the query string /might/ trick the browser in thinking 
> that this is not a static page, and force it to re-request it from the 
> server.
> 
By the way, forget the above, it does not work, or only partially.
The browser does not serve it from its cache the second time, but the 
server sends a 304 anyway.
(I tried with http://tomcat.apache.org)

What I wrote about the browser plugin and looking at the 
request/response header sequence is still true however, and is very 
instructive.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applying a servlet filter to a static resource

Posted by André Warnier <aw...@ice-sa.com>.
Caldarale, Charles R wrote:
>> From: Matt Veitas [mailto:mveitas@gmail.com]
>> Subject: Applying a servlet filter to a static resource
>>
>> I am using Tomcat 6.0.24 and am trying to use a servlet filter 
>> on a static resource and am having some troubles as the 2nd 
>> request returns a 304.
> 
> Are you sure it's Tomcat returning the 304, or is that being done by some intermediary?
> 
>> unfortunately I can't change the calling url.
> 
> What is the URL of interest?  Under what name is the webapp deployed?
> 
>> <filter-mapping>
>>    <filter-name>TestFilter</filter-name>
>>    <url-pattern>/js/config/my.json</url-pattern>
>> </filter-mapping>
> 
> The above should give the filter control, if the <url-pattern> is correct.
> 
>> Is there anything I can do to make Tomcat not return the 
>> 304 for the 2nd request to my.json?
> 
> The 304 is generated in Tomcat's DefaultServlet; your filter should be getting control before that servlet is invoked, if it's configured properly and there's nothing between the client and Tomcat that's caching the request.  Try verifying that the subsequent requests are actually making it to Tomcat (enable the AccessLogValve in server.xml).
> 

What also helps a lot usually in such cases, is using a browser add-on 
such as HttpFox (Firefox) or Fiddler2 (IE), to see the exact sequence of 
HTTP requests/responses, and their associated HTTP headers.
For example, you would see there if it is really the server sending a 
304 (not modified), or the browser just serving the document from its 
own cache.
Or you would see if the response comes from Tomcat or from Apache httpd 
or IIS or some other proxy, if you have one of those in front of Tomcat 
(as seen from the browser end).

To the browser, the above URI "/(webappname)/js/config/my.json" 
certainly looks like a static resource, so it would not be surprising if 
it just cached it the first time, and served it from cache the second time.

I have not tested the following, and it is not based on any certain 
knowledge based on the HTTP RFCs, so treat with caution, but you could 
try a cheap trick :
In the link to that "document" in the html page, add a dummy query 
string, like :
instead of
http://servername/webappname/js/config/my.json
try
http://servername/webappname/js/config/my.json?a=my.json

The presence of the query string /might/ trick the browser in thinking 
that this is not a static page, and force it to re-request it from the 
server.

(And the reason for which I am adding the "=my.json" at the very end, is 
to get over a nasty habit of IE browsers, who try to "evaluate" the URL 
and guess themselves what the content might be based on the "extension", 
instead of believing what the server tells them in the Content-type header).



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Applying a servlet filter to a static resource

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Matt Veitas [mailto:mveitas@gmail.com]
> Subject: Applying a servlet filter to a static resource
> 
> I am using Tomcat 6.0.24 and am trying to use a servlet filter 
> on a static resource and am having some troubles as the 2nd 
> request returns a 304.

Are you sure it's Tomcat returning the 304, or is that being done by some intermediary?

> unfortunately I can't change the calling url.

What is the URL of interest?  Under what name is the webapp deployed?

> <filter-mapping>
>    <filter-name>TestFilter</filter-name>
>    <url-pattern>/js/config/my.json</url-pattern>
> </filter-mapping>

The above should give the filter control, if the <url-pattern> is correct.

> Is there anything I can do to make Tomcat not return the 
> 304 for the 2nd request to my.json?

The 304 is generated in Tomcat's DefaultServlet; your filter should be getting control before that servlet is invoked, if it's configured properly and there's nothing between the client and Tomcat that's caching the request.  Try verifying that the subsequent requests are actually making it to Tomcat (enable the AccessLogValve in server.xml).

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applying a servlet filter to a static resource

Posted by André Warnier <aw...@ice-sa.com>.
Pid wrote:
> On 11/03/2010 14:38, Matt Veitas wrote:
...
>> If I
>> could modify the calling code to hit a servlet instead of the static
>> resource, I could use that servlet to read the static resource (JSON),
>> perform some business logic and modify the JSON, and return that to the
>> client, but unfortunately I can't change the calling url.
>>
I missed this the first time around, but you can do that, and you would 
not have to modify the calling URL.

Use a servlet instead of a filter, and (to my knowledge) there is 
nothing stopping you from mapping your servlet to

      <url-pattern>/js/config/my.json</url-pattern>

In order for the above to map exactly to the URL
http://yourhost/js/config/my.json
I think you would have to put this servlet inside the ROOT (default) webapp.
Then make sure that when this servlet returns the response, it sets the 
appropriate HTTP headers to forbid the client browser (or any caching 
entity in-between) from caching the result.
I do not remember all the headers that play a role there, but two of 
them are
Expires: 0   (or better, the date of yesterday in the correct format)
Cache-Control: no-cache

Check in RFC2616, sections 13 and 14



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Applying a servlet filter to a static resource

Posted by Pid <pi...@pidster.com>.
On 11/03/2010 14:38, Matt Veitas wrote:
> Hi,
>
> I am using Tomcat 6.0.24 and am trying to use a servlet filter on a static
> resource and am having some troubles as the 2nd request returns a 304.

What are you trying to do in the Filter?  Post some code.

>If I
> could modify the calling code to hit a servlet instead of the static
> resource, I could use that servlet to read the static resource (JSON),
> perform some business logic and modify the JSON, and return that to the
> client, but unfortunately I can't change the calling url.
>
> <filter>
>     <filter-name>TestFilter</filter-name>
>     <filter-class>foo.TestFilter</filter-class>
> </filter>
> <filter-mapping>
>     <filter-name>TestFilter</filter-name>
>     <url-pattern>/js/config/my.json</url-pattern>
> </filter-mapping>
>
> Is there anything I can do to make Tomcat not return the 304 for the 2nd
> request to my.json?

Are you setting 'no-cache' headers or modifying the content of the file?


p



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org