Eliminate the cache action Return Value Problem of tomcat in Ajax

Source: Internet
Author: User
When writing Ajax, we will use a timer to call an action regularly to detect something. For example, in EC 2.0, we use Ajax technology to regularly check the user's message inbox and check whether there are new messages and how many messages there are. However, in practical applications, we found that Tomcat will cache the returned values of this check message class. That is to say, if three new messages are detected, then the subsequent Ajax call Will, regardless of whether the number of new messages in the database has changed, Tomcat returns the same data to JSP, that is, three messages.

This is a very annoying phenomenon, because Tomcat thinks that each request is the same URL, and naturally it caches the return value of this action. The only way is to make the URLs of each Ajax request different so that Tomcat's cache behavior can be eliminated.

So, the most likely thing to think of is to add the current time after the URL, because time is always passing, so after adding the time, each URL will be different, the cache behavior of Tomcat can be eliminated. In JavaScript, we do this:

Function getmsgnotify (){
VaR dateinstance = new date ();
VaR url = "<% = webcontextroot %>" + "/tomodule. ec? Prefix =/portal & page =/getmsgnotify. EC & Counter = "+ dateinstance. gettime ();
Request. Open ("get", URL, true );
Request. onreadystatechange = updatepage;
Request. Send (null );
} The getmsgpolicy function will be called regularly. Note that VaR datainstance = new date (); the Code must be written in the method called regularly, this ensures that the current time is obtained each time this function is called. If this code is put out, the code will be executed only once, the natural time is the earliest time. Note: In JavaScript, when a new date object is added, the current time is returned without a parameter.

Then we use the gettime function. This function returns the difference (in seconds) between the date time we get and the time in 197x, which ensures that each URL is different.

Before that, I didn't use this method. I declare a global variable counter. The initial value is 0. After each getmsgpolicy operation, I add this counter to 1, to generate different URLs, but this does not work, because if the user does not close the browser, every time the page is loaded, the counter starts counting from 0, in this way, the current URL will be generated in the same situation as the previous one (all counted from 0 )! Therefore, the scientific approach is still the one above, using time to generate a URL that will never be the same.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.