pradeep cs
pradeep cs

Reputation: 523

how to avoid reloading js and css in jsp

I have few same js and css files and i used to load from different jsps. If already css and js loaded, we need to not load files again. But those files are reloading. And giving 200 Success code in firebug console. it should give 302 success code

i can hit the url directly. I dont want to call js and css dynamically.

load the jsp content

css and js loading done through relative path. i couldnt copy the code. Because it is not allowing here

If any additional information require, pls update

Upvotes: 1

Views: 1506

Answers (1)

Vineet Reynolds
Vineet Reynolds

Reputation: 76709

Assuming that you are referencing the CSS and JS files from your JSPs as static references in <script> and <link> tags, you can avoid the scenario where the browser is provided with these files on every request by setting HTTP headers when you first serve them.

Without setting appropriate expiration times or "freshness" guarantees for resources, browsers will typically end up issuing for a fresh copy of the resource every time, even though the resource may be present in the browse cache. Note, that if the browser cache is disabled, then you cannot, as a web-programmer do anything about the browser's behavior, unless you get your users to enable the browser cache; until then, the browser will request for fresh copies of resources every single time. These requests may be served by a proxy cache, but it is not necessary that such a cache exist.

Setting resource expiration times or freshness guarantees in Java web applications can be accomplished by either of the following:

  • Configuring the HTTP server, or the Web Application server (if that is possible) to set Expires and Cache-Control headers in the HTTP responses generated for the CSS and JS files or mimetypes. Configuration changes that ought to be done would vary from server to server and are too expansive to list here.
  • Writing a Servlet filter for your web application that will intercept all responses for URLs ending with *.js and *.css and add HTTP Expires and Cache-Control headers to the responses. This option is less preferable especially if you wish to have the same amount of configurability that is afforded by web and application servers; you will need to provide any configuration options via init parameters for the filter in your web.xml file. Also, changes to the configuration will require a restart of the application containing the filter.

If you need to understand the values to be specified for the Expires and Cache-Control headers, then I would recommend that you read the "Caching Tutorial for Web Authors and Webmasters", which provides details on how to use the Expires and Cache-Control headers and how they affect caching behavior.

Suggested values for the expiration times specified in the Expires header or the max-age value in the Cache-Control header, range from a few minutes to a few hours, depending on how often you change the site content. You must also avoid specifying Cache-Control headers with no-cache and no-store values, for they would often end up issuing requests for the same resource on the next attempt.

Upvotes: 3

Related Questions