Cookie クラス
名前空間
使用方法
PageReference クラスの setCookies メソッドを使用して、ページに Cookie を添付します。
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>Cookie コンストラクタ
Cookie のコンストラクタは次のとおりです。
Cookie メソッド
Cookie のメソッドは次のとおりです。すべてインスタンスメソッドです。
getMaxAge()
Cookie の有効期間を示す秒単位の数字が返されます。< 0 を設定すると、セッション Cookie が発行されます。0 を設定すると、Cookie は削除されます。
署名
public Integer getMaxAge()
戻り値
型: Integer
isSecure()
Cookie が HTTPS でのみアクセス可能な場合、true を返します。それ以外の場合は、false を返します。
署名
public Boolean isSecure()
戻り値
型: Boolean