antun
10-03-2003, 09:48 AM
HTTP requests in Laszlo will forward cookie information automatically, so you can use an intermediary .jsp or other servlet to store session information in the servlet container (e.g. Tomcat).
All of the work gets done in a server-side script (in this case, mysession.jsp). The Laszlo app queries that script using a dataset. Since this is a very simple example, mysession.jsp will always return the same XML:
<response session_value="somevalue" />
... where somevalue is the value of the String "session_value" that resides in the HTTPSession object, or "Not set.", if none has yet been set. mysession.jsp can also be used to set the variable to a given value, by making a request through the same dataset. In a more complex app, you could have multiple datasets, and multiple server-side scripts, all dealing with the same session.
session.lzx
<canvas width="240">
<debug y="150" />
<dataset name="myDataset"
src="mysession.jsp" type="http" autorequest="true" />
<simplelayout axis="y" spacing="15" />
<text width="200" x="25" multiline="true">
The current value of the session variable "session_value" is:
</text>
<text name="variable_value" align="center" resize="true"
datapath="myDataset:/response/@session_value" />
<text width="200" x="25" multiline="true">
Enter new value for "session_value":
</text>
<windowtext name="user_value" align="center" />
<button align="center">Set session value
<method event="onclick">
<![CDATA[
var valueToSet = user_value.getText();
user_value.setText( "" );
myDataset.setQueryString( "page_action=setValue&session_value="
+ valueToSet );
myDataset.doRequest();
]]>
</method>
</button>
<text align="center" fgcolor="blue">
<a href="http://www.laszlosystems.com/"><u>www.laszlosystems.com</u></a>
</text>
</canvas>
mysession.jsp
<%
/*
mysession.jsp
Read or set a servlet session variable. If nothing is specified on the
query string, then this will return any cookies set. If the page_action
GET Parameter is set to setValue, then a value can be set too.
*/
// Obtain any instructions from the querystring
//
String page_action = "getValue";
String session_value = "";
if ( request.getQueryString() != null ) {
page_action = request.getParameter( "page_action" );
session_value = request.getParameter( "session_value" );
}
if ( page_action.equals("getValue") ) {
if ( session.getAttribute("session_value") != null ) {
session_value = (String) session.getAttribute("session_value");
} else {
session_value = "Not set.";
}
} else if ( page_action.equals("setValue") ) {
session.setAttribute("session_value", session_value);
}
%><response session_value="<%=session_value%>"/>
Try clicking on the www.laszlosystems.com link, and then using the back button on the browser to return to the app. The session cookie will still be set.
Enjoy!
All of the work gets done in a server-side script (in this case, mysession.jsp). The Laszlo app queries that script using a dataset. Since this is a very simple example, mysession.jsp will always return the same XML:
<response session_value="somevalue" />
... where somevalue is the value of the String "session_value" that resides in the HTTPSession object, or "Not set.", if none has yet been set. mysession.jsp can also be used to set the variable to a given value, by making a request through the same dataset. In a more complex app, you could have multiple datasets, and multiple server-side scripts, all dealing with the same session.
session.lzx
<canvas width="240">
<debug y="150" />
<dataset name="myDataset"
src="mysession.jsp" type="http" autorequest="true" />
<simplelayout axis="y" spacing="15" />
<text width="200" x="25" multiline="true">
The current value of the session variable "session_value" is:
</text>
<text name="variable_value" align="center" resize="true"
datapath="myDataset:/response/@session_value" />
<text width="200" x="25" multiline="true">
Enter new value for "session_value":
</text>
<windowtext name="user_value" align="center" />
<button align="center">Set session value
<method event="onclick">
<![CDATA[
var valueToSet = user_value.getText();
user_value.setText( "" );
myDataset.setQueryString( "page_action=setValue&session_value="
+ valueToSet );
myDataset.doRequest();
]]>
</method>
</button>
<text align="center" fgcolor="blue">
<a href="http://www.laszlosystems.com/"><u>www.laszlosystems.com</u></a>
</text>
</canvas>
mysession.jsp
<%
/*
mysession.jsp
Read or set a servlet session variable. If nothing is specified on the
query string, then this will return any cookies set. If the page_action
GET Parameter is set to setValue, then a value can be set too.
*/
// Obtain any instructions from the querystring
//
String page_action = "getValue";
String session_value = "";
if ( request.getQueryString() != null ) {
page_action = request.getParameter( "page_action" );
session_value = request.getParameter( "session_value" );
}
if ( page_action.equals("getValue") ) {
if ( session.getAttribute("session_value") != null ) {
session_value = (String) session.getAttribute("session_value");
} else {
session_value = "Not set.";
}
} else if ( page_action.equals("setValue") ) {
session.setAttribute("session_value", session_value);
}
%><response session_value="<%=session_value%>"/>
Try clicking on the www.laszlosystems.com link, and then using the back button on the browser to return to the app. The session cookie will still be set.
Enjoy!