Java Interview 14| Session and Cookie

Source: Internet
Author: User

1, in the distributed environment, the management session is usually used in the following three ways:

(1) Session Replication mode management (i.e. session copy)

Copy the session data broadcast from one machine to the rest of the machine in the cluster

Usage scenario: Fewer machines, less network traffic

Advantages: Simple implementation, less configuration, no impact on user access when a machine is down in the network

Disadvantage: Broadcast replication to the rest of the machine when there is a certain degree, bring a certain network overhead

Tomcat's own cluster based on TCP broadcast technology

(2) Session Sticky mode Management

The sticky session, when the user accesses a machine in the cluster, forces the specified subsequent requests to fall on this machine

Usage Scenario: Moderate number of machines, not very demanding stability requirements

Advantages: Easy to implement, easy to configure, no additional network overhead

Disadvantage: The network has a machine down, the user session will be lost, easy to cause a single point of failure

Nginx can use rewrite technology to locate a request to a machine, or a consistent hash algorithm can be used to improve stability (when a certain server is down, only partial cache invalidation)

(3) Centralized cache management

The session is stored on a machine in a distributed cache cluster, and the session information is taken from the cache when the user accesses the different nodes.

Usage Scenario: Multiple machines in a cluster, complex network environment

Advantages: Good Reliability

Disadvantage: the implementation of complexity, stability depends on the stability of the cache, session information into the cache should have a reasonable policy to write

① Sticky Session Processing method

Different tomcat specifies access to different master memcached. The information between multiple memcached is synchronous, and can be backed up and highly available. When the user accesses, the session is first created in Tomcat, and then a copy of the session is placed on its corresponding memcahed. Memcache only backup, read and write on Tomcat. When a tomcat hangs, the cluster accesses the user's access to the backup Tomcat, and then, based on the SessionID stored in the cookie, locates the session, fails to find it, and then goes to the corresponding memcached to go to the session. After it is found, copy it to the backup Tomcat.

② non-sticky Session processing method

Principle: memcached from Copy, write session to write from memcached service, read from main memcached, Tomcat itself does not store session

Reference Blog: http://blog.csdn.net/u010028869/article/details/50773174?ref=myread

2. The difference and connection between session and Cookie

1. The cookie data is stored on the client's browser and the session data is placed on the server.

2, the cookie is not very safe, because session is to save the data on the server side, just pass a sessionid through the cookie, so the session is more suitable to store user's privacy and important content.

3. How to handle cookies when the client is disabled?

Session URL rewrite to ensure that the session mechanism can still be used when the client is disabled or does not support cookies. The session mechanism is a server-side mechanism that uses a hash list (HASHMAP)-like structure (or a hash table) to hold information.

When a program needs to create a session for a client's request, the server first checks to see if the client's request contains a session ID (called the session ID.), and if it is included, it has previously created a session for this client. The server will retrieve the session by the session ID (not retrieved, a new one will be created). If the client request does not contain a session ID, a session is created for this client and a value for the session Id,session ID associated with this session is generated should be a string that is neither duplicated nor easily found to mimic the pattern. This session ID will be returned to the client in this response to be saved. This session ID can be saved by using a cookie, so that the browser can automatically play the logo to the server during the interactive process. Generally the name of this cookie is similar to Seeesionid. However, a cookie can be artificially banned, and there must be other mechanisms that can still pass the session ID back to the server when the cookie is banned. A technique that is often used is called URL rewriting, which attaches the session ID directly behind the URL path.

There is also a technique called form-hidden fields. Is that the server automatically modifies the form, adding a hidden field so that the session ID can be passed back to the server when the form is submitted. Like what:

<form name= "Testform" action= "/xxx" > <input type= "hidden" name= "Jsessionid" value= " byok3vjfd75apnrf7c2hmdnv6qzcebzwowibyenlerjq99zwpbng!-145788764 "> <input type=" text "> </form>

4. How to handle cookies when the client is disabled?

  

Java Interview 14| Session and Cookie

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.