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">
|
|
|