<?xml version="1.0" encoding="Shift_JIS"?>
<!--
  Copyright 2003 The Apache Software Foundation.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  $Id: LinkTool.xml,v 1.4 2005/01/20 14:48:46 nose Exp $
-->
<document>

    <properties>
        <title>LinkTool</title>
        <author email="sidler@apache.org">Gabriel Sidler</author>
        <translator email="nekop@programmers.jp">木村 貴由</translator>
        <translator email="nose@intellilink.co.jp">野瀬 直樹</translator>
        <projectfile>xdocs-ja/view/menu.xml</projectfile>
        <original>LinkTool</original>
     </properties>

    <body>

    <section name="LinkTool Reference Documentation" alias="LinkTool リファレンスドキュメント">

        <primary>
        <p>The LinkTool provides methods to work with URIs:</p>
        </primary>
        <p>LinkTool はURIに関するメソッドを提供します:</p>
        <primary>
        <ul>
            <li>set server relative URIs<a href="#setRelative()">
              setRelative()</a></li>
            <li>set arbitrary URIs<a href="#setURI()">
              setURI()</a></li>
            <li>construct and encode query strings, see methods <a href="#addQueryData()">
              addQueryData()</a> and <a href="#getQueryData()">getQueryData()</a></li>
            <li>set internal anchor references<a href="#setAnchor()">
              setAnchor()</a></li>
            <li>encode session ID into URL, see method <a href="#toString()">toString()</a></li>
            <li>retrieve server, port and path info for the current request, see methods
              <a href="#getURI()">getURI()</a>, <a href="#getContextURL()">getContextURL()</a>, 
              <a href="#getContextPath()">getContextPath()</a> and 
              <a href="#getBaseRef()">getBaseRef()</a></li>
        </ul>
        </primary>
        <ul>
            <li>サーバ相対 URI を設定します。 <a href="#setRelative()">
              setRelative()</a></li>
            <li>任意の URI を設定します。 <a href="#setURI()">
              setURI()</a></li>
            <li>クエリストリングの構築とエンコードを行います。
            <a href="#addQueryData()">addQueryData()</a> と
            <a href="#getQueryData()">getQueryData()</a> メソッドを参照してください。</li>
            <li>内部アンカリファレンスを設定する <a href="#setAnchor()">
              setAnchor()</a></li>
            <li>URL へセッションIDをエンコードします。
            <a href="#toString()">toString()</a> メソッドを参照してください。</li>
            <li>サーバ名とポートとパスを処理中のリクエストへ格納します。
              <a href="#getURI()">getURI()</a>, <a href="#getContextURL()">getContextURL()</a>, 
              <a href="#getContextPath()">getContextPath()</a> と 
              <a href="#getBaseRef()">getBaseRef()</a> メソッドを参照してください。</li>
        </ul>

        <primary>
        <p>The LinkTool is somewhat special in that many of its methods return
        a new instance of LinkTool. This facilitates greatly the repeated use
        of the LinkTool in Velocity and leads to an elegant syntax.</p> 
        </primary>
        <p>LinkTool の持つ多くのメソッドが、新しい LinkTool のインスタンスを返却するという点は多少特殊です。
        これは、 LinkTool の再利用を促進するとともに、エレガントな構文を提供します。</p> 

        <primary>
        <p>I've been struggling a bit to find the right terminology for the things
        that this tool works with. Based on document 
        <a href="http://www.w3.org/Architecture/Terms">http://www.w3.org/Architecture/Terms</a>,
        I have used:</p>
        </primary>
        <p>私は、このツールが処理するものに適切な用語を見つけようと努力しました。
        <a href="http://www.w3.org/Architecture/Terms">http://www.w3.org/Architecture/Terms</a>、をドキュメントのベースとして利用しました。
        用語はこれらを使用します:</p>
        
        <primary>
        <dl>
            <dt>URI</dt>
            <dd>To refer to the full, absolute address of web resources, e.g. static documents
            or abstract resources like dynamically generated content. 
            Example: <code>http://myserver.org/myapp/templates/index.html</code></dd>

            <dt>URI Reference</dt>
            <dd>To refer to partial and relative URIs that reference a URI. Example: 
            <code>templates/index.html</code>. </dd>

            <dt>URL</dt>
            <dd>Not used here.</dd>

        </dl>
        </primary>
        <dl>
            <dt>URI</dt>

            <dd>静的ドキュメントや、ダイナミックに生成されるコンテンツのような抽象的なリソースなどの
                Web リソースの完全な絶対アドレスを指します。
            例: <code>http://myserver.org/myapp/templates/index.html</code></dd>

            <dt>URI リファレンス</dt>
            <dd>URIを参照するための部分的・相対的なURIを指します。
            例: <code>templates/index.html</code></dd>

            <dt>URL</dt>
            <dd>使用しません。</dd>

        </dl>
        
        <toolinfo>
            <version>@@@version@@@, @@@date@@@</version>
            <clazz>org.apache.velocity.tools.view.tools.LinkTool</clazz>
            <name>$link</name>
            <author email="sidler@teamup.com">Gabriel Sidler</author>
            <author email="nathan@esha.com">Nathan Bubna</author>
            <config-example>&lt;tool&gt;
  &lt;key&gt;link&lt;/key&gt;
  &lt;scope&gt;request&lt;/scope&gt;
  &lt;class&gt;org.apache.velocity.tools.view.tools.LinkTool&lt;/class&gt;
&lt;/tool&gt;</config-example>
        </toolinfo>

        <methods/>

    </section>


    <section name="setURI()">
        <primary>
        <method name="setURI()">
    
            <abstract>
                Returns a copy of this LinkTool instance with the given URI reference.
            </abstract>
    
            <signature>
                LinkTool setURI(String uri)
            </signature>
            
            <parameters>
                <parameter name="uri">
                    URI reference to set. The URI reference can be absolute 
                    (<code>http://www.mydomain.com/myapp/templates/index.html</code>),
                    server-relative (<code>/myapp/templates/index.html</code>), 
                    relative (<code>templates/index.html</code>) 
                    and may contain query strings (<code>templates/index.html&#63;key1=value1&amp;key2=value2</code>).
                </parameter>
            </parameters>
    
            <returns>
                A new instance of <code>LinkTool</code>.  
            </returns>
    
            <description>
                <p>No conversions are applied to the given URI reference. This method will 
                overwrite any previously set URI reference but will copy query 
                data set with method <code>setQueryData()</code>.</p>
             </description>
           
<sourcecode>## a relative reference
$link.setURI("index.html").addQueryData("key1","val 1")

## a server-relative reference
$link.setURI("/myapp/index.vm").addQueryData("key1","val 1")

## an absolute reference
$link.setURI("http://myserver.org/myapp/index.vm")
.addQueryData("key1", "val 1")

## a reference with a query string
$link.setURI("/myapp/index.vm?key0=val0")
.addQueryData("key1", "val 1")
</sourcecode>

<p>This produces:</p>

<sourcecode>index.vm/key1=val+1
    
/myapp/index.vm/key1=val+1

http://myserver.org/myapp/index.vm?key1=val+1

/myapp/index.vm?key0=val0&amp;key1=val+1</sourcecode>

        </method>
        </primary>
        <method name="setURI()">
    
            <abstract>
                与えられた URI リファレンスを、この LinkTool のインスタンスのコピーへ設定して返却します。
            </abstract>
    
            <signature>
                LinkTool setURI(String uri)
            </signature>
            
            <parameters>
                <parameter name="uri">
                    設定する URI リファレンス。
                    URI リファレンスは絶対パス  (<code>http://www.mydomain.com/myapp/templates/index.html</code>), サーバ相対パス (<code>/myapp/templates/index.html</code>), 相対パス (<code>templates/index.html</code>) であり、クエリストリングを含めることもできます (<code>templates/index.html&#63;key1=value1&amp;key2=value2</code>)。
                </parameter>
            </parameters>
    
            <returns>
                <code>LinkTool</code> の新しいインスタンス。
            </returns>
    
            <description>
                <p>与えられた URI リファレンスには何の変換も行われません。
                このメソッドは前に設定されていた URI リファレンスを上書きしますが、
                クエリデータは<code>setQueryData()</code> メソッドを使用してコピーされます。</p>
             </description>
           
<sourcecode>## 相対参照
$link.setURI("index.html").addQueryData("key1","val 1")

## サーバ相対参照
$link.setURI("/myapp/index.vm").addQueryData("key1","val 1")

## 絶対参照
$link.setURI("http://myserver.org/myapp/index.vm")
.addQueryData("key1", "val 1")

## クエリストリングを付加した参照
$link.setURI("/myapp/index.vm?key0=val0")
.addQueryData("key1", "val 1")
</sourcecode>

<p>これは以下を出力します:</p>

<sourcecode>index.vm/key1=val+1
    
/myapp/index.vm/key1=val+1

http://myserver.org/myapp/index.vm?key1=val+1

/myapp/index.vm?key0=val0&amp;key1=val+1</sourcecode>

        </method>
    </section>

    <section name="setRelative()">
        <primary>
        <method name="setRelative()">
    
            <abstract>
                Returns a copy of this LinkTool instance with the specified context-relative
                URI reference converted to a server-relative URI reference.
            </abstract>
    
            <signature>
                LinkTool setRelative(String uri)
            </signature>
            
            <parameters>
                <parameter name="uri">
                    A context-relative URI reference, i.e. a URI reference that is relative to the URI
                    used to address this web application.
                </parameter>
            </parameters>
    
            <returns>
                A new instance of <code>LinkTool</code>.  
            </returns>
    
            <description>
                <p>The specified context-relative URI reference is converted to a server-relative URI reference.
                This method will overwrite any previously set URI reference but will copy the query string.</p>
                
<sourcecode>
## a context-relative hyperlink
&lt;a href="$link.setRelative("templates/index.vm").addQueryData("key1","val 1")"&gt;My Link&lt;/a&gt;
</sourcecode>

                <p>Produces something like:</p>

<sourcecode>&lt;a href="/myapp/templates/index.vm?key=val+1"&gt;My Link&lt;/a&gt;</sourcecode>
                
            </description>
    
        </method>
        </primary>
        <method name="setRelative()">
    
            <abstract>
                指定されたコンテキスト相対 URI リファレンスをサーバ相対 URI リファレンスへ変換し、この LinkTool のインスタンスのコピーへ設定して返却します。
            </abstract>
    
            <signature>
                LinkTool setRelative(String uri)
            </signature>
            
            <parameters>
                <parameter name="uri">
                    コンテキスト相対 URI リファレンス。
                    つまり、 Web アプリケーションからの相対 URI。
                </parameter>
            </parameters>
    
            <returns>
                <code>LinkTool</code> の新しいインスタンス。
            </returns>
    
            <description>
                <p>指定されたコンテキスト相対 URI リファレンスはサーバ相対 URI リファレンスへ変換されます。
                このメソッドは前に設定されていた URI リファレンスを上書きしますが、
                クエリデータはコピーされます。</p>
                
<sourcecode>
## コンテキスト相対リンク
&lt;a href="$link.setRelative("templates/index.vm").addQueryData("key1","val 1")"&gt;My Link&lt;/a&gt;
</sourcecode>

                <p>以下のように出力されます:</p>

<sourcecode>&lt;a href="/myapp/templates/index.vm?key=val+1"&gt;My Link&lt;/a&gt;</sourcecode>
                
            </description>
    
        </method>
    </section>


    <section name="addQueryData()">
        <primary>
        <method name="addQueryData()">
    
            <abstract>
                Adds a key=value pair to the URI reference.
            </abstract>
    
            <signature>
                LinkTool addQueryData(String key, Object value)
            </signature>
            
            <parameters>
                <parameter name="key">
                    Key (name) of query parameter.
                </parameter>

                <parameter name="value">
                    Value of the query parameter. The string representation of this object is 
                    added to the query data.
                </parameter>
            </parameters>
    
            <returns>
                A new instance of <code>LinkTool</code>.  
            </returns>
    
            <description>
                <p>Adds a key=value pair to the query data. This returns a new LinkTool 
                containing both a copy of this LinkTool's query data and the new data.
                This makes repeated use in Velocity templates easy. Query data is
                URL-encoded before it is appended.</p>
                
                <p>See <a href="#setURI()">method setURI()</a> for examples.</p>
             </description>
    
        </method>
        </primary>
        <method name="addQueryData()">
    
            <abstract>
                URI リファレンスに key=value のペアを追加します。
            </abstract>
    
            <signature>
                LinkTool addQueryData(String key, Object value)
            </signature>
            
            <parameters>
                <parameter name="key">
                    クエリパラメータのキー(名前)。
                </parameter>

                <parameter name="value">
                    クエリパラメータの値。
                    このオブジェクトの文字列表現がクエリデータに付加されます。
                </parameter>
            </parameters>
    
            <returns>
                <code>LinkTool</code> の新しいインスタンス。
            </returns>
    
            <description>
                <p>クエリデータに key=value のペアを追加します。
                このメソッドは元の LinkTool のクエリデータと新規のデータ、
                両方を含む新しい LinkTool を返却します。
                これによって Velocity テンプレートにおいて[訳者補足：LinkTool
                インスタンスを]繰り返し利用することが容易になります。
                クエリデータは追加される前に URL エンコードされます。</p>
                
                <p>例は <a href="#setURI()">メソッド setURI()</a> を参照してください。</p>
             </description>
    
        </method>
    </section>


    <section name="setAnchor()">
        <primary>
        <method name="setAnchor()">
    
            <abstract>
                Sets an internal document reference to append to this link (e.g. #foo).
            </abstract>
    
            <signature>
                LinkTool setAnchor(String anchor)
            </signature>
            
            <parameters>
                <parameter name="anchor">
                    The internal document reference to append to this link
                </parameter>
            </parameters>
    
            <returns>
                A new instance of <code>LinkTool</code>.  
            </returns>
    
            <description>
                <p>Returns a copy of the link with the specified anchor to be 
                added to the end of the generated hyperlink.  This returns a new LinkTool 
                containing both a copy of this LinkTool's data and the new anchor value.
                This makes repeated use in Velocity templates easy. The anchor value
                is url encoded before being rendered.</p>
             </description>
    
        </method>
        </primary>
        <method name="setAnchor()">
    
            <abstract>
                内部ドキュメントへのリファレンスをこのリンクへ追加します (例 #foo)。
            </abstract>
    
            <signature>
                LinkTool setAnchor(String anchor)
            </signature>
            
            <parameters>
                <parameter name="anchor">
                    リンクへ追加する内部ドキュメントへのリファレンス。
                </parameter>
            </parameters>
    
            <returns>
                <code>LinkTool</code> の新しいインスタンス。
            </returns>
    
            <description>
                <p>生成されるリンクの最後に指定したアンカを追加した、
                リンクのコピーを返却します。このメソッドは元の LinkTool の
                データと、新しいアンカの値を含む、新規の LinkTool を返却します。
                これによって Velocity テンプレートにおいて[訳者補足：LinkTool
                インスタンスを]繰り返し利用することが容易になります。
                アンカの値は表示される前に URL エンコードされます。</p>
             </description>
    
        </method>
    </section>


    <section name="getURI()">
        <primary>
        <method name="getURI()">
    
            <abstract>
                Returns the current URI reference.
            </abstract>
    
            <signature>
                LinkTool getURI()
            </signature>
            
            <returns>
                Returns the current URI reference as set by one of the methods 
                setURI() or setRelative(). Any conversions 
                have been applied. The returned URI reference does not included query data that was
                added with method addQueryData().
            </returns>
    
            <see>
                Method <a href="#getQueryData()"><code>getQueryData()</code></a> to retrieve
                query data only. Method <a href="#toString()"><code>toString()</code></a> to 
                retrieve the URI reference including all query data.
            </see>
            
            <description>
<sourcecode>#set ($l = $link.setForward("start").addQueryData("key1","val 1"))
&lt;a href="$l.getURI()"&gt;My Link&lt;/a&gt;</sourcecode>

<p>Produces something like:</p>

<sourcecode>&lt;a href="/myapp/templates/index.vm"&gt;My Link&lt;/a&gt;</sourcecode>

            </description>
        </method>
        </primary>
        <method name="getURI()">
    
            <abstract>
                現在の URI リファレンスを返却します。
            </abstract>
    
            <signature>
                LinkTool getURI()
            </signature>
            
            <returns>
                setURI() や setRelative() などのメソッドによって設定された現在の URI リファレンスを返却します。
                必要な変換は全て行われます。
                返却される URI リファレンスは addQueryData() メソッドで付加されたクエリデータを含みません。
            </returns>
    
            <see>
                クエリデータのみを返却する <a href="#getQueryData()"><code>getQueryData()</code></a> メソッド。
                クエリデータを含む URI リファレンスを返却する <a href="#toString()"><code>toString()</code></a> メソッド。
            </see>
            
            <description>
<sourcecode>#set ($l = $link.setForward("start").addQueryData("key1","val 1"))
&lt;a href="$l.getURI()"&gt;My Link&lt;/a&gt;</sourcecode>

<p>以下のように出力されます:</p>

<sourcecode>&lt;a href="/myapp/templates/index.vm"&gt;My Link&lt;/a&gt;</sourcecode>

            </description>
        </method>
    </section>


    <section name="getQueryData()">
        <primary>
        <method name="getQueryData()">
    
            <abstract>
                Returns this instance's query data.
            </abstract>
    
            <signature>
                String getQueryData()
            </signature>
                
            <returns>
                This instance's query data as set by the method 
                <a href="#setQueryData()"><code>setQueryData()</code></a>. The
                returned String is URL-encoded, e.g. "key=value&amp;foo=this+is+encoded".
            </returns>
            <description>
<sourcecode>#set ($l = $link.setForward("start").addQueryData("key1","val 1"))
$l.getQueryData()</sourcecode>

<p>Produces:</p>

<sourcecode>key1=val+1</sourcecode>

            </description>
                
        </method>
        </primary>
        <method name="getQueryData()">
    
            <abstract>
                このインスタンスのクエリデータを返却します。
            </abstract>
    
            <signature>
                String getQueryData()
            </signature>
                
            <returns>
                <a href="#setQueryData()"><code>setQueryData()</code></a> メソッドで設定されたこのインスタンスのクエリデータ。
                返却される String は URL エンコードされたものです。
                例: "key=value&amp;foo=this+is+encoded"。
            </returns>
            <description>
<sourcecode>#set ($l = $link.setForward("start").addQueryData("key1","val 1"))
$l.getQueryData()</sourcecode>

<p>出力:</p>

<sourcecode>key1=val+1</sourcecode>

            </description>
                
        </method>
    </section>



    <section name="getContextURL()">
        <primary>
        <method name="getContextURL()">
    
            <abstract>
                Returns the URI that addresses this web application.
            </abstract>
    
            <signature>
                String getContextURL()
            </signature>
    
            <returns>
                Returns the URI reference that addresses this web application, e.g. 
                "http://myserver.net/myapp". This string does not end with a "/".
            </returns>
    
            <description>
                <p>Note! This will not represent the URI reference or 
                query data set for this LinkTool instance.</p>

<sourcecode>$link.getContextURL()</sourcecode>

<p>Produces something like:</p>

<sourcecode>http://myserver.net/myapp</sourcecode>

            </description>
    
        </method>
        </primary>
        <method name="getContextURL()">
    
            <abstract>
                この Web アプリケーションを示す URI を返却します。
            </abstract>
    
            <signature>
                String getContextURL()
            </signature>
    
            <returns>
                このウェブアプリケーションを示す URI を返却します。
                例: "http://myserver.net/myapp"。
                返却される文字列の最後には "/" が付加されません。
            </returns>
    
            <description>
                <p>注意! これはこの LinkTool インスタンスの URI リファレンスやクエリデータを表すものではありません。</p>

<sourcecode>$link.getContextURL()</sourcecode>

<p>以下のように出力されます:</p>

<sourcecode>http://myserver.net/myapp</sourcecode>

            </description>
    
        </method>
    </section>



    <section name="getContextPath()">
        <primary>
        <method name="getContextPath()">
    
            <abstract>
                Returns the context path that addresses this web application.
            </abstract>
    
            <signature>
                String getContextPath()
            </signature>
                
            <returns>
                The context path that addresses this web application, e.g. 
                <code>"/myapp"</code>. This string starts with a "/" but does 
                not end with a "/".
            </returns>
    
            <description>
                <p> Note! This will not represent the URI reference or query 
                data set for this LinkTool instance.</p>

<sourcecode>$link.getContextPath()</sourcecode>

<p>Produces something like:</p>

<sourcecode>/myapp</sourcecode>

            </description>
    
        </method>
        </primary>
        <method name="getContextPath()">
    
            <abstract>
                このウェブアプリケーションを示すコンテキストパスを返却します。
            </abstract>
    
            <signature>
                String getContextPath()
            </signature>
                
            <returns>
                このウェブアプリケーションを示すコンテキストパス。
                例: <code>"/myapp"</code>。
                返却される文字列は "/" で始まりますが、最後には "/" が付加されません。
            </returns>
    
            <description>
                <p>注意! これはこの LinkTool インスタンスの URI リファレンスやクエリデータを表すものではありません。</p>

<sourcecode>$link.getContextPath()</sourcecode>

<p>以下のように出力されます:</p>

<sourcecode>/myapp</sourcecode>

            </description>
    
        </method>
    </section>



    <section name="getBaseRef()">
        <primary>
        <method name="getBaseRef()">
    
            <abstract>
                Returns the full URI of this template.
            </abstract>
    
            <signature>
                String getBaseRef()
            </signature>
                
            <returns>
                The full URI of this template without any query data.
                e.g. <code>http://myserver.net/myapp/templates/View.vm</code>.
            </returns>
    
            <description>
                <p>Note! The returned String will not represent the URI reference or query 
                data set for this LinkTool instance. A typical application of this method 
                is with the HTML base tag.</p>

<sourcecode>## a base tag
&lt;base href="$link.getBaseRef()"&gt;</sourcecode>

<p>Produces something like:</p>

<sourcecode>&lt;base href="http://myserver.net/myapp/templates/index.vm"&gt;</sourcecode>

            </description>
    
        </method>
        </primary>
        <method name="getBaseRef()">
    
            <abstract>
                このテンプレートの完全 URI を返却します。
            </abstract>
    
            <signature>
                String getBaseRef()
            </signature>
                
            <returns>
                クエリデータを含まないこのテンプレートの完全 URI 。
                例: <code>http://myserver.net/myapp/templates/View.vm</code>。
            </returns>
    
            <description>
                <p>注意! これはこの LinkTool インスタンスの URI リファレンスやクエリデータを表すものではありません。
                このメソッドは通常、 HTML の base タグに使用します。</p>

<sourcecode>## base タグ
&lt;base href="$link.getBaseRef()"&gt;</sourcecode>

<p>以下のように出力されます:</p>

<sourcecode>&lt;base href="http://myserver.net/myapp/templates/index.vm"&gt;</sourcecode>

            </description>
    
        </method>
    </section>




    <section name="toString()">
        <primary>
        <method name="toString()">
    
            <abstract>
                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.
            </abstract>
    
            <signature>
                String toString()
            </signature>
            
            <returns>
                The full URI that has been built with this tool
                e.g. <code>/myapp/stuff/View.vm?id=42&amp;type=blue#foobar</code>.
            </returns>
    
            <description>
                <p>Typically it is not necessary to call this method explicitly.
                Velocity will call the toString() method automatically to 
                obtain a representable version of objects.</p>

<sourcecode>&lt;a href="$link.setRelative("demo").addQueryString("key1", "val 1")"&gt;
My Link&lt;/a&gt;</sourcecode>

<p>Produces something like:</p>

<sourcecode>&lt;a href="/myapp/demo.do?key1=val+1"&gt;My Link&lt;/a&gt;</sourcecode>

<p>If sessions are used and the web client does not support cookies, the 
<code>toString()</code> method automatically encodes the session ID into the 
returned URI. The above example would then produce something like: </p>

<sourcecode>&lt;a href="/myapp/demo.do;jsessionid=aaaaaaanisPWVYEY01?key1=val+1"&gt;
My Link&lt;/a&gt;</sourcecode>



<p><strong>A Note about URI Encoding</strong></p>


<p>URI encoding is about encoding the session ID into the URI string. This
section briefly explains the reasoning behind it and how it works.</p>

<p>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.</p>

<p>The Servlet API has forseen two mechanisms to identify HTTP requests that 
belong to a particular session.</p>

<ul>
  <li><strong>Cookies</strong>: 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.</li> 
  <li><strong>URI Encoding</strong>: 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.</li>
</ul>

<p>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.</p>

<p>The Servlet API offers two methods to support the web application
developer with the URI encoding:</p>
<pre>
java.lang.String encodeURL(java.lang.String url)
java.lang.String encodeRedirectURL(java.lang.String url)
</pre>

<p>These two methods encode the sesssion id into the URI string <strong>if</strong> sessions
are used <strong>and if</strong> the particular web client does not support cookies.</p>

<p>The <code>toString()</code> method of LinkTool automatically does URI encoding using the
<code>encodeURL()</code> 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.</p>

<p>The following examples show the output of the <code>toString()</code> method if 
cookies are enabled and disabled.</p>

<sourcecode>&lt;a href="$link.setURI("MyPage.vm").addQueryData("key1","val 1")"&gt;
My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" action="$link.setURI("MyPage.vm")"&gt;</sourcecode>


<p>Produces this if cookies are enabled:</p>

<sourcecode>&lt;a href="MyPage.vm?key1=val+1"&gt;My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" action="MyPage.vm"&gt;</sourcecode>


<p>Produces something like this if cookies are diabled:</p>

<sourcecode>&lt;a href="MyPage.vm;jsessionid=E9833012F7B2F8570963B137?key1=val+1"&gt;
My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" 
action="MyPage.vm;jsessionid=E9833012F7B20857096F37743B137"&gt;</sourcecode>

            </description>
    
        </method>
        </primary>
        <method name="toString()">
    
            <abstract>
                構築された完全な URI を返却します。
                クッキーをサポートしないウェブクライアントに対しては、セッション ID を URL の中に含めます。
            </abstract>
    
            <signature>
                String toString()
            </signature>
            
            <returns>
                このツールで構築された完全な URI を返却します。
                例: <code>/myapp/stuff/View.vm?id=42&amp;type=blue#foobar</code>。
            </returns>
    
            <description>
                <p>通常、このメソッドを明示的に呼ぶ必要はありません。
                Velocity は、オブジェクトの文字列表現を取得するために toString() メソッドを自動的に呼びだします。</p>

<sourcecode>&lt;a href="$link.setRelative("demo").addQueryString("key1", "val 1")"&gt;
My Link&lt;/a&gt;</sourcecode>

<p>以下のように出力されます:</p>

<sourcecode>&lt;a href="/myapp/demo.do?key1=val+1"&gt;My Link&lt;/a&gt;</sourcecode>

<p>ウェブクライアントがクッキーをサポートしない場合にセッションを使用すると、 <code>toString()</code> メソッドは自動的にセッションIDを、返却する URI へ含めます。
上の例は以下のようなものを生成します: </p>

<sourcecode>&lt;a href="/myapp/demo.do;jsessionid=aaaaaaanisPWVYEY01?key1=val+1"&gt;
My Link&lt;/a&gt;</sourcecode>



<p><strong>URI エンコーディングについて</strong></p>


<p>URI エンコーディング とはセッションIDを URI 文字列中へ含めるものです。
この章では、この理由と、これがどのような動きとなるかについて簡単に説明します。</p>

<p>多くの Web アプリケーションは、特定のユーザとアプリケーションの状態を関連づけるためにセッションを利用します。
例えば、ユーザがオンラインショップをブラウジングしている間、ショッピングカートの状態を保持するためにセッションが利用されます。</p>

<p>Servlet API には、特定のセッションに属する HTTP リクエストを識別するための、2つの仕組みがあります。</p>

<ul>
  <li><strong>クッキー</strong>: セッションの開始時に、
    セッションIDを含む新しいクッキーがクライアントに送られます。
    クライアントはすべてのリクエストにこのクッキーをを付加して返却します。</li> 
  <li><strong>URI エンコーディング(訳者注: URL Rewriting、URL 書き換え)</strong>: 
    セッションIDは URI へエンコードされます。
    サーバはリクエストの URI を解析し、セッションIDを検出します。</li>
</ul>

<p>多くの開発者は、セッションの識別にクッキーを使用することを選びます。
すべてのURIをエンコードする、ということが不要なので、クッキーベースの仕組みの方が簡単だからです。
しかし、セキュリティやプライバシーの問題から、ブラウザのクッキーのサポートを無効にしているユーザも存在します。
セッション管理がクッキーのみで行われている場合、上記のようなユーザに対してはセッション管理ができません。
こういったケースでも、良い設計がされているウェブアプリケーションならば、フォールバック(訳者注: 代替)として URI エンコーディングによる方法が機能します。</p>

<p>Servlet API は URI エンコーディングをサポートする2つのメソッドをウェブアプリケーション開発者へ提供しています。</p>
<pre>
java.lang.String encodeURL(java.lang.String url)
java.lang.String encodeRedirectURL(java.lang.String url)
</pre>

<p>この2つのメソッドは、<strong>もし</strong>、セッションを使用していて、<strong>かつ</strong>、ウェブクライアントがクッキーをサポートしていない場合に、セッションIDを URI に含めます。</p>

<p>LinkTool の <code>toString()</code> メソッドは <code>encodeURL()</code> メソッドを使用し、自動的に URI エンコーディングを行います。
したがって、アプリケーション中のすべての URI が LinkTool によって生成されている場合、アプリケーションはクライアントのクッキーのサポート有無にかかわらず、適切に動作するでしょう。</p>

<p>以下はクッキーが有効/無効時の <code>toString()</code> メソッドの出力例です。</p>

<sourcecode>&lt;a href="$link.setURI("MyPage.vm").addQueryData("key1","val 1")"&gt;
My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" action="$link.setURI("MyPage.vm")"&gt;</sourcecode>


<p>クッキー有効時の出力:</p>

<sourcecode>&lt;a href="MyPage.vm?key1=val+1"&gt;My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" action="MyPage.vm"&gt;</sourcecode>


<p>クッキー無効時の出力はこのようになります:</p>

<sourcecode>&lt;a href="MyPage.vm;jsessionid=E9833012F7B2F8570963B137?key1=val+1"&gt;
My Link&lt;/a&gt;

&lt;form name="MyForm" method="post" 
action="MyPage.vm;jsessionid=E9833012F7B20857096F37743B137"&gt;</sourcecode>

            </description>
    
        </method>
    </section>

 </body>
</document>


