How to solve the inconsistency between the changing session values of Ajax access and the difference between GET and POST in HTTP, ajaxsession

Source: Internet
Author: User

How to solve the inconsistency between the changing session values of Ajax access and the difference between GET and POST in HTTP, ajaxsession

Today, when I made a progress bar, I encountered a problem. I stored a counter in the session. When I crawled a piece of data, this value was + 1, then the front-end obtains the value of the session every 3 seconds, but the problem arises. the value obtained under FF is normal, but it is always the previous value under IE, the latest session value can be obtained only when the page is re-opened:

The following is my proBar. jsp code:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-"%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <script type = "text/javascript" src = "<% = path %>/js/jquery. js "> </script> <script type =" text/javascript "src =" <% = path %>/js/jquery. progressbar. min. js "> </script> <script type =" text/javascript "> Function createXMLHttpRequest () {var xmlHttp; if (window. activeXObject) {xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");} else {xmlHttp = new XMLHttpRequest ();} return xmlHttp;} function btn_click () {var xmlHttp; xmlHttp = createXMLHttpRequest (); xmlHttp. onreadystatechange = processor; // xmlHttp. open ("GET", "/jbbs/servlet/ControlCrawl? Method = getinforsize ", true); xmlHttp. open (" POST ","/jbbs/servlet/ControlCrawl? Method = getinforsize ", true); xmlHttp. send (null); function processor (){.......}} var action = setInterval ("btn_click ();",); </script> <span> crawling progress: </span> <br/> <div id = "container"> </div>

Later, it would be normal to change GET to POST.

PS: GET and POST of HTTP

Http defines different methods for interaction with the server. There are four basic methods: GET, POST, PUT, and DELETE. The full name of a URL is a resource descriptor. We can think of it as: a URL address, which is used to describe resources on a network, while GET, POST, PUT, DELETE corresponds to the query, modify, add, and DELETE operations on this resource. Here, you should have a rough understanding. GET is generally used to obtain/query resource information, while POST is generally used to update resource information.

1. According to HTTP specifications, GET is used for information retrieval, and should be secure and idempotent. 

(1). The so-called security means that the operation is used to obtain information rather than modify information. In other words, GET requests generally do not have side effects. That is to say, it only obtains the resource information, just like the database query. It does not modify, add data, and does not affect the resource status.

* Note: security only indicates that the information is not modified.

(2) idempotence means that multiple requests to the same URL should return the same result.

Idempotence (idempotent and idempotence) is a mathematical or computer concept that is common in abstract algebra.

Idempotence can be defined as follows:   

For a single-object operation, if an operation is performed multiple times for all the numbers in the range, the result is the same as that obtained once, this operation is called idempotent. For example, an absolute value operation is an example. In a real number set, abs (a) = abs (a) is used )).

For binary operations, it is required that when the two values involved in the calculation are equivalent, if the calculation result is equal to the two values involved in the calculation, the operation is called the idempotence, for example, a function that calculates the maximum values of two numbers has the power in the real number set, that is, max (x, x) = x.

However, in practice, the above two rules are not so strict. Example of referencing others' articles: for example, the front pages of news sites are constantly updated. Although the second request will return a different batch of news, this operation is still considered safe and idempotent because it always returns the current news. Basically, if the target is to open a link, the user can be sure that the resource is not changed from his own perspective.

2. According to HTTP specifications, POST indicates requests that may modify resources on the server.

Continue to reference the above example: for news websites, readers should POST their comments on news, because the Site Resources are different after the comments are submitted, or the resource is modified.

 However, in practice, many people fail to follow the HTTP specification, which leads to many reasons, such:

1. Many users are greedy and convenient. GET is used to update resources, because FORM is required for POST, which may cause a little trouble.

2. You can add, DELETE, modify, and query resources through GET/POST without using PUT and DELETE.

3. in addition, early Web MVC Framework designers did not consciously treat and design URLs as abstract resources, therefore, a serious problem is that the traditional Web MVC framework basically only supports the GET and post http methods, rather than the PUT and DELETE methods.

* About MVC:

MVC originally exists in the Desktop program, M is the exponential data model, V is the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different expressions.

The old style (not strictly compliant with the HTTP specification) is described as a typical example at the last three points. With the development of the architecture, there is now a RepresentationalState Transfer and a new style that supports the HTTP specification, for more information, see RESTful Web Services.

Let's look at the difference between GET and POST on the surface: 

(1 ). first, "the data submitted in GET mode can only be 1024 bytes". Because GET submits data through a URL, the amount of data that can be submitted by GET is directly related to the URL length. In reality, there is no parameter ceiling for the URL. the HTTP protocol specification does not limit the URL length. This restriction is imposed by specific browsers and servers. The length of the URL is limited to 2083 bytes (2 K + 35) by IE ). For other browsers, such as Netscape and FireFox, there is no length limit theoretically. The limit depends on the support of the operating system. Note that this limit is the length of the entire URL, not just the length of your parameter value.

(2 ). theoretically, there is no size limit on POST and no size limit on HTTP specifications. it is inaccurate to say that "the size of POST data is limited to 80 K/K, there is no limit on POST data, and the restriction is the processing capability of the server's processing program.

For ASP programs, there is a K data length limit when the Request object processes each form field. However, if Request. BinaryRead is used, there is no such restriction.

With this extension, Microsoft has increased its restrictions for IIS 6.0 for security reasons.

Note:

1). By default, IIS 6.0 has a maximum asp post data volume of KB, and each form field is limited to kb.

2). By default, IIS 6.0 uploads a file up to 4 MB.

3). By default, the maximum request header of IIS 6.0 is 16 kb.

These restrictions are not available before IIS 6.0.

Therefore, the 80 K and K values above may only be the default values (Note: I have not confirmed the IIS4 and IIS5 parameters), but they must be set by myself. Because the default values of these parameters are different for IIS in each version, refer to the relevant IIS configuration documents for details.

3. In ASP, the server uses Request. QueryString to obtain GET Request Parameters and Request. Form to obtain POST Request parameters.

In JSP, request. getParameter (\ "XXXX \"), although the jsp also has a request. the getQueryString () method is difficult to use, for example, passing a test. jsp? Name = hyddd & password = hyddd. What you get with request. getQueryString () is: name = hyddd & password = hyddd. In PHP, you can use $ _ GET and $ _ POST to obtain data in GET and POST respectively, while $ _ REQUEST can obtain data in GET and POST requests. It is worth noting that using $ _ request in REQUEST and PHP in JSP poses a risk. Next time I will write a summary article.

4. POST is more secure than GET.

Note: The security mentioned here is not the same as the "Security" mentioned in GET. The above "Security" only means not to modify the data, but here the meaning of Security is the true meaning of Security, for example: submit data through GET, the username and password will appear in the URL in plain text, because (1) the logon page may be cached by the browser, (2) others can view the historical records of the browser, then others can GET your account and password. In addition, using GET to submit data may also cause Cross-site request forgery attacks.

To sum up, Get is a request to request data from the server, while Post is a request to submit data to the server. In FORM, the default Method is "GET". In essence, GET and POST are only different sending mechanisms, not a single sending!

The above is a small Editor to share with you the problem of inconsistent values of the changing session for Ajax access and the difference between GET and POST in the HTTP protocol. I hope it will be useful to you, for more information about ajax session inconsistency, please stay tuned to this site.

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.