|
Returns the full URI that has been constructed. The session ID
is encoded into the URL if cookies are not supported by the web client.
- Returns
-
The full URI that has been built with this tool
e.g.
/myapp/stuff/View.vm?id=42&type=blue#foobar.
Typically it is not necessary to call this method explicitly.
Velocity will call the toString() method automatically to
obtain a representable version of objects.
<a href="$link.setRelative("demo").addQueryString("key1", "val 1")">
My Link</a>
|
|
Produces something like:
<a href="/myapp/demo.do?key1=val+1">My Link</a>
|
|
If sessions are used and the web client does not support cookies, the
toString() method automatically encodes the session ID into the
returned URI. The above example would then produce something like:
<a href="/myapp/demo.do;jsessionid=aaaaaaanisPWVYEY01?key1=val+1">
My Link</a>
|
|
A Note about URI Encoding
URI encoding is about encoding the session ID into the URI string. This
section briefly explains the reasoning behind it and how it works.
Many web applications use sessions to associate an application state
with a particualar user. For example, a session might be used to maintain
the state of a shopping cart while the user is browsing the online shop.
The Servlet API has forseen two mechanisms to identify HTTP requests that
belong to a particular session.
- Cookies: A new cookie containing the session ID is sent
to the client at the beginning of a session. The client returns this cookie
with every request.
- URI Encoding: The session ID is encoded into the
URI string. The server parses the URI of requests to detect the presence
of an encoded session ID.
Most developers prefer to use cookies to identify sessions. The cookie-based
mechanism is easier to work with because it does not require the
encoding of every URI. However,
for reasons of security and privacy some users choose to disable cookie
support in their browsers. If session management relies on cookies only,
it will fail in such a situation. A well designed web application needs to
be able to fall back to the URI encoding method in this case.
The Servlet API offers two methods to support the web application
developer with the URI encoding:
java.lang.String encodeURL(java.lang.String url)
java.lang.String encodeRedirectURL(java.lang.String url)
These two methods encode the sesssion id into the URI string if sessions
are used and if the particular web client does not support cookies.
The toString() method of LinkTool automatically does URI encoding using the
encodeURL() method.
Therefore, if all URIs within an application are produced with the LinkTool, the application
is able to work properly with or without cookie support of the client.
The following examples show the output of the toString() method if
cookies are enabled and disabled.
<a href="$link.setURI("MyPage.vm").addQueryData("key1","val 1")">
My Link</a>
<form name="MyForm" method="post" action="$link.setURI("MyPage.vm")">
|
|
Produces this if cookies are enabled:
<a href="MyPage.vm?key1=val+1">My Link</a>
<form name="MyForm" method="post" action="MyPage.vm">
|
|
Produces something like this if cookies are diabled:
<a href="MyPage.vm;jsessionid=E9833012F7B2F8570963B137?key1=val+1">
My Link</a>
<form name="MyForm" method="post"
action="MyPage.vm;jsessionid=E9833012F7B20857096F37743B137">
|
|
構築された完全な URI を返却します。
クッキーをサポートしないウェブクライアントに対しては、セッション ID を URL の中に含めます。
- Returns
-
このツールで構築された完全な URI を返却します。
例:
/myapp/stuff/View.vm?id=42&type=blue#foobar。
通常、このメソッドを明示的に呼ぶ必要はありません。
Velocity は、オブジェクトの文字列表現を取得するために toString() メソッドを自動的に呼びだします。
<a href="$link.setRelative("demo").addQueryString("key1", "val 1")">
My Link</a>
|
|
以下のように出力されます:
<a href="/myapp/demo.do?key1=val+1">My Link</a>
|
|
ウェブクライアントがクッキーをサポートしない場合にセッションを使用すると、 toString() メソッドは自動的にセッションIDを、返却する URI へ含めます。
上の例は以下のようなものを生成します:
<a href="/myapp/demo.do;jsessionid=aaaaaaanisPWVYEY01?key1=val+1">
My Link</a>
|
|
URI エンコーディングについて
URI エンコーディング とはセッションIDを URI 文字列中へ含めるものです。
この章では、この理由と、これがどのような動きとなるかについて簡単に説明します。
多くの Web アプリケーションは、特定のユーザとアプリケーションの状態を関連づけるためにセッションを利用します。
例えば、ユーザがオンラインショップをブラウジングしている間、ショッピングカートの状態を保持するためにセッションが利用されます。
Servlet API には、特定のセッションに属する HTTP リクエストを識別するための、2つの仕組みがあります。
- クッキー: セッションの開始時に、
セッションIDを含む新しいクッキーがクライアントに送られます。
クライアントはすべてのリクエストにこのクッキーをを付加して返却します。
- URI エンコーディング(訳者注: URL Rewriting、URL 書き換え):
セッションIDは URI へエンコードされます。
サーバはリクエストの URI を解析し、セッションIDを検出します。
多くの開発者は、セッションの識別にクッキーを使用することを選びます。
すべてのURIをエンコードする、ということが不要なので、クッキーベースの仕組みの方が簡単だからです。
しかし、セキュリティやプライバシーの問題から、ブラウザのクッキーのサポートを無効にしているユーザも存在します。
セッション管理がクッキーのみで行われている場合、上記のようなユーザに対してはセッション管理ができません。
こういったケースでも、良い設計がされているウェブアプリケーションならば、フォールバック(訳者注: 代替)として URI エンコーディングによる方法が機能します。
Servlet API は URI エンコーディングをサポートする2つのメソッドをウェブアプリケーション開発者へ提供しています。
java.lang.String encodeURL(java.lang.String url)
java.lang.String encodeRedirectURL(java.lang.String url)
この2つのメソッドは、もし、セッションを使用していて、かつ、ウェブクライアントがクッキーをサポートしていない場合に、セッションIDを URI に含めます。
LinkTool の toString() メソッドは encodeURL() メソッドを使用し、自動的に URI エンコーディングを行います。
したがって、アプリケーション中のすべての URI が LinkTool によって生成されている場合、アプリケーションはクライアントのクッキーのサポート有無にかかわらず、適切に動作するでしょう。
以下はクッキーが有効/無効時の toString() メソッドの出力例です。
<a href="$link.setURI("MyPage.vm").addQueryData("key1","val 1")">
My Link</a>
<form name="MyForm" method="post" action="$link.setURI("MyPage.vm")">
|
|
クッキー有効時の出力:
<a href="MyPage.vm?key1=val+1">My Link</a>
<form name="MyForm" method="post" action="MyPage.vm">
|
|
クッキー無効時の出力はこのようになります:
<a href="MyPage.vm;jsessionid=E9833012F7B2F8570963B137?key1=val+1">
My Link</a>
<form name="MyForm" method="post"
action="MyPage.vm;jsessionid=E9833012F7B20857096F37743B137">
|
|
|