1# Managing Cookies and Data Storage 2 3 4## Cookie Management 5 6A cookie is a segment of data sent from the server to the client to uniquely identify a user during network access. The client may hold the data and provide it to the server at later interactions so that the server can quickly identify the client identity and status. 7 8The **Web** component provides the **WebCookieManager** class for you to manage cookie information, which is stored in the **/proc/{pid}/root/data/storage/el2/base/cache/web/Cookiesd** file in the application sandbox path. 9 10The following uses [configCookieSync()](../reference/apis-arkweb/js-apis-webview.md#configcookiesync11) as an example to describe how to set a cookie's value to **value=test** for **www\.example.com**. For details about functions and usage of other APIs, see [WebCookieManager()](../reference/apis-arkweb/js-apis-webview.md#webcookiemanager). 11 12 13```ts 14// xxx.ets 15import web_webview from '@ohos.web.webview'; 16import business_error from '@ohos.base'; 17 18@Entry 19@Component 20struct WebComponent { 21 controller: web_webview.WebviewController = new web_webview.WebviewController(); 22 23 build() { 24 Column() { 25 Button('configCookieSync') 26 .onClick(() => { 27 try { 28 web_webview.WebCookieManager.configCookieSync('https://www.example.com', 'value=test'); 29 } catch (error) { 30 let e: business_error.BusinessError = error as business_error.BusinessError; 31 console.error(`ErrorCode: ${e.code}, Message: ${e.message}`); 32 } 33 }) 34 Web({ src: 'www.example.com', controller: this.controller }) 35 } 36 } 37} 38``` 39 40 41## Cache and Storage Management 42 43Network resource requests are relatively time-consuming during website access. You can store resources locally by means of **cache** and **Dom Storage** to fasten the access to the same website. 44 45 46### Cache 47 48Use [cacheMode()](../reference/apis-arkweb/ts-basic-components-web.md#cachemode) to configure the cache mode for page resources. Four cache modes are supported: 49 50- **Default**: Page resources in a cache that has not expired are preferentially used. If the cache does not exist, page resources are obtained from the network. 51 52- **None**: Page resources are loaded from the cache. If the cache does not exist, page resources are obtained from the network. 53 54- **Online**: Page resources are not loaded from the cache. All resources are obtained from the network. 55 56- **Only**: Page resources are only loaded from the cache. 57 58 59In the following example, the cache mode is set to **None**. 60 61 62 63```ts 64// xxx.ets 65import web_webview from '@ohos.web.webview'; 66 67@Entry 68@Component 69struct WebComponent { 70 @State mode: CacheMode = CacheMode.None; 71 controller: web_webview.WebviewController = new web_webview.WebviewController(); 72 build() { 73 Column() { 74 Web({ src: 'www.example.com', controller: this.controller }) 75 .cacheMode(this.mode) 76 } 77 } 78} 79``` 80 81 82 To obtain up-to-date resources, you can use [removeCache()](../reference/apis-arkweb/js-apis-webview.md#removecache) to clear cached resources. The sample code is as follows: 83 84```ts 85// xxx.ets 86import web_webview from '@ohos.web.webview'; 87import business_error from '@ohos.base'; 88 89@Entry 90@Component 91struct WebComponent { 92 @State mode: CacheMode = CacheMode.None; 93 controller: web_webview.WebviewController = new web_webview.WebviewController(); 94 build() { 95 Column() { 96 Button('removeCache') 97 .onClick(() => { 98 try { 99 // If this parameter is set to true, the cache in both the ROM and RAM is cleared. If this parameter is set to false, only the cache in the RAM is cleared. 100 this.controller.removeCache(true); 101 } catch (error) { 102 let e: business_error.BusinessError = error as business_error.BusinessError; 103 console.error(`ErrorCode: ${e.code}, Message: ${e.message}`); 104 } 105 }) 106 Web({ src: 'www.example.com', controller: this.controller }) 107 .cacheMode(this.mode) 108 } 109 } 110} 111``` 112 113 114### Dom Storage 115 116Dom Storage falls into Session Storage and Local Storage. Wherein, Session Storage applies to the temporary data, and its data storage and release follow the session lifecycle; Local Storage applies to the persistent data, which is flushed to the application directory. In both storage modes, data is stored in a form of key-value pair, and is usually used when a page that needs to be stored on the client is accessed. You can use [domStorageAccess()](../reference/apis-arkweb/ts-basic-components-web.md#domstorageaccess) to enable Dom Storage. The following is the sample code: 117 118 119 120```ts 121// xxx.ets 122import web_webview from '@ohos.web.webview'; 123 124@Entry 125@Component 126struct WebComponent { 127 controller: web_webview.WebviewController = new web_webview.WebviewController(); 128 build() { 129 Column() { 130 Web({ src: 'www.example.com', controller: this.controller }) 131 .domStorageAccess(true) 132 } 133 } 134} 135``` 136