views:

368

answers:

1

One of the tip given by both Google and Yahoo! to speed up webpages loading is to configure a cookieless subdomain to server static content.

How do you configure a "cookieless subdomain" using Tomcat in standalone mode (this question is not about how to use Apache to serve static content in a cookieless-way, but about how to do it in Tomcat-standalone mode)?

Note that I don't care about filters supporting If-Modified-Since nor care about filters supporting gzipping: the static content I'm serving is forever cacheable (or its name will change) and it is already compressed data (so gzip would only slow down the transfer).

Do I need two different Tomcat webapps? (one "cookiefull" and one "cookieless")

Do I need two different servlets? (as of now I've got only one dispatcher/controller servlet).

Why would a "regular" link to, say, a static image be called in a cookiefull way when it would be on the same domain as the main webapp and then be called in a "cookie-less" way when it is on a subdomain?

I don't understand exactly what is going on: is it the browser that decides to append or not cookies to the query? If so, why would it not append the cookies to a static query on a "cookieless" subdomain.

Any example as to what is going on behind the scene is most welcome :)

+2  A: 

You just need to setup another domain. It can point to the same webapps. Just make sure you don't drop cookies on this new domain.

For example, you main site is

  www.example.com

and you can have another domain

  static.example.com 

For your cookie-less static resources. They can point to the same hosts, go to the same Servlet. You just need to make sure in your app that you don't drop cookies for these static contents.

EDIT: Assuming your static content is served by the default servlet in Tomcat, you don't have to do anything. It doesn't drop cookies for you so your new domain should be cookie free.

If you have to process the static content in the same servlet, you can do something like this,

   if (!request.getServerName().equals("static.example.com")) {
       // Drop cookie
   }

This example assumes you don't drop cookie in your root domain (.example.com). If you do that, you have to get another domain, like examplecdn.com.

ZZ Coder
@ZZ Coder: ah, my question wasn't clear enough. I know how to create sub-domains. It's the *"just make sure you (don't) drop cookies on this new domain"* that I don't get: what decides if I "drop" or not cookies on that domain? The browser? How does it decide to "drop" cookies on one domain and not on the other? :)
Webinator
See my edits ....
ZZ Coder