Caching is an effective means for reducing load on web servers, especially for those that dynamically generate web documents in J2EE applications. While adding caching to the application server can greatly reduce response times for requests, the logic to ensure consistency with the backend database requires considerable effort to develop. Much of the complexity is in minimizing unnecessary page invalidations, a key goal for improving the cache hit rate and response times. In this paper we explore a range of invalidation policies that are progressively more precise. A contribution of this work is in achieving precise invalidations at the application server layer automatically. To explore these issues, we introduce AutoWebCache, a system for adding caching for dynamic content automatically to J2EE server-side applications having a back-end database. It uses aspect oriented programming for injecting the cache code automatically into an application, but this method requires the SQL query analysis-- needed for invalidation-- be performed at run-time, which is costly. We demonstrate how to eliminate this run-time analysis overhead through effective caching of intermediate analysis results. In two e-commerce benchmarks, RUBiS and TPC-W, we show our method can be highly effective, reducing the response times, 63% and 97%, respectively.