Cookie sharing between projects

Address: https://blog.csdn.net/smq29661318/article/details/51025905

For some large development project, a project might be split into several sub-projects to develop, but between certain items with certain pages between the inlet can jump to each other, and therefore, for some session state also need to share, such as internationalization, after switching subsystem a locale to jump to subsystem B, the user first enters the system locale when a B and a jump page should be before a system is consistent with the general development process, the language information will be stored in the Cookie or Session can even be a third-party cache, such as Redis, today to talk about how to achieve Cookie sharing between projects.
For example there are two projects will be executed this ajax, this role is to interface data to the server, the incoming data corresponding JavaBean-based annotation check JSR303, and the results returned to the client, returned the language in which information is stored in the cookie is based on the language of the decision.
 $.ajax({      url: "http://localhost:8888/ssm/interfaces/test/m006",      type: "POST",      async: false,      dataType: "json",      contentType: "application/json",      cache: false,      data: JSON.stringify({        "id": "bgdsdgs",        "name": "name",         "email": "[email protected]",        "voModel2": [{          "id": "[email protected]",          "v3": {"id":"sdfghjkl"}        }]      }),      success: function(data) {        console.log(data.errorDetail);        $("#aa").html(JSON.stringify(data));      },      error: function() {      }    })1234567891011121314151617181920212223给出一个设置cookie的js方法,为了说明原理,不用jquery-cookie等第三方库
function setCookie (c_name, value, expiredays ) {var exdate = new Date (); exdate.setDate (exdate.getDate () + expiredays); document.cookie = c_name + "=" + escape (value) + ((expiredays == null) "": ";? expires =" + exdate.toGMTString ());} 1234561, first perform setCookie (console in the page under the first project of the "lang", "en_US") , add the name lang, en_US value of the cookie 
this time more out of a cookie 
this time to refresh the page, the information obtained is information in English 

2, then in the second direct access interface the same page, but the emergence of the information is still Chinese. 
Although access to the same interface, and are used to determine the language of the current cookie value, and the value of the cookie is the same, why two Chinese side is on the other side of it is in English. According to the above can be seen, in addition to the Cookie name, value, expire and other information, as well as domain and path attributes, the current domain is the domain, the default address for the request, such as the URL of www.jb51.net/test/test. aspx, then the domain default is www.jb51.net, path default is the root directory of the current project, and domain as long as there is a path different, it means cross-domain, can not be shared, and the domain may be the same between different projects, and path It must be different. It can also be set with the domain and path set cookie when, in order to achieve sharing, both projects should be set to the same. Cookie settings modify the above method, what specific settings as the case may be
function setCookie (c_name, value, expiredays) {var exdate = new Date (); exdate.setDate (exdate.getDate () + expiredays); document.cookie = c_name + "=" + escape (value) + ((expiredays == null) "": ";? expires =" + exdate.toGMTString ()) + "; path = /" + "; domain = localhost";} 12345678 achieved so far shared the cookie, the cookie is now a project to change the language environmental information will follow with another project changed. ps: 1, for the next cookie, the name of the same situation, if the path, a different domain, cookie still two can coexist, and take the cookie default is the smallest of the range, that is, under the current path and domain, so no way other domain and path will be the following cookie. 2, java cookie provided a method
        Cookie cookie = new Cookie ( "lang ", "zh_CN"); // Set domain cookie.setDomain ( "localhost"); // set path cookie.setPath ( "/"); // set after the document is no longer in the front end see the cookie, improves security cookie.setHttpOnly (true); // expiration time, in seconds, is less than 0 indicates // stored in memory, close the browser cookie disappeared, // equal to 0 immediately delete the cookie // greater than 0 on the hard disk cookie.setMaxAge (1); cookie // create HTTPS connection can only be transmitted in a browser session to the server for validation cookie.setSecure (to false); 1,234,567,891,011,121,314
----- ---------------- author: cat had a Mia source: CSDN original: https: //blog.csdn.net/smq29661318/article/details/51025905 copyright: This article is bloggers original article, reproduced, please attach Bowen link!

Guess you like

Origin www.cnblogs.com/YanSmallKind/p/11276955.html