この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Cookie クラス

Cookie クラスにより、Apex を使用して Force.com サイトの Cookie にアクセスできます。

名前空間

System

使用方法

PageReference クラスsetCookies メソッドを使用して、ページに Cookie を添付します。
  • Apex の Cookie 名と値セットは URL 符号化されています。つまり、@ などの文字は % 記号および 16 進数表現に置き換えられます。
  • setCookies メソッドは Cookie 名にプレフィックス「apex__」を追加します。
  • Cookie の値を null に設定すると、期限切れの属性の設定ではなく、空の文字列値の Cookie を送信します。
  • Cookie の作成後は、Cookie のプロパティを変更することはできません。
  • 機密情報を Cookie に格納する場合は注意してください。Cookie の値に関係なくページはキャッシュされます。動的なコンテンツを生成するために Cookie の値を使用する場合は、ページキャッシュを無効にする必要があります。詳細は、Salesforce オンラインヘルプのForce.com サイトページのキャッシュ」を参照してください。

重要

Cookie クラスを使用する場合は、次の制限に留意してください。
  • Cookie クラスには、Salesforce API バージョン 19 以降を使用して保存されている Apex を使用することでのみアクセスできます。
  • Force.com ドメインごとに設定できる Cookie の最大数はブラウザにより異なります。新しいブラウザは古いブラウザより高い制限が設定されています。
  • Cookie は名前および属性を含め 4K 未満である必要があります。

サイトの詳細は、Salesforce オンラインヘルプの「Force.com Sites の概要」を参照してください。

次の例では、CookieController クラスを作成します。このクラスは Visualforce ページ (下記マークアップを参照) を使用して、ユーザにページが表示されるたびにカウンタが更新されます。ページへのアクセス回数が Cookie に保存されます。

1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// A Visualforce controller class that creates a cookie
18// used to keep track of how often a user displays a page
19public class CookieController {
20
21    public CookieController() {
22        Cookie counter = ApexPages.currentPage().getCookies().get('counter');
23    
24        // If this is the first time the user is accessing the page, 
25        // create a new cookie with name 'counter', an initial value of '1', 
26        // path 'null', maxAge '-1', and isSecure 'false'. 
27        if (counter == null) {
28            counter = new Cookie('counter','1',null,-1,false);
29        } else {
30        // If this isn't the first time the user is accessing the page
31        // create a new cookie, incrementing the value of the original count by 1
32            Integer count = Integer.valueOf(counter.getValue());
33            counter = new Cookie('counter', String.valueOf(count+1),null,-1,false);
34        }
35    
36        // Set the new cookie for the page
37        ApexPages.currentPage().setCookies(new Cookie[]{counter});
38    }
39
40    // This method is used by the Visualforce action {!count} to display the current 
41    // value of the number of times a user had displayed a page. 
42    // This value is stored in the cookie.
43    public String getCount() {
44        Cookie counter = ApexPages.currentPage().getCookies().get('counter');
45        if(counter == null) {
46            return '0';
47        }
48        return counter.getValue();
49    }
50}
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17// Test class for the Visualforce controller
18@isTest
19private class CookieControllerTest {
20  // Test method for verifying the positive test case
21  static testMethod void testCounter() {
22    //first page view
23    CookieController controller = new CookieController();
24    System.assert(controller.getCount() == '1');
25
26    //second page view       
27    controller = new CookieController();
28    System.assert(controller.getCount() == '2');
29  }
30}
次は、上記の CookieController Apex コントローラを使用する Visualforce ページです。アクション {!count} では、上記のコントローラで getCount メソッドをコールします。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="CookieController">
18You have seen this page {!count} times
19</apex:page>