FW: FW: [Laszlo-dev] sessionrpc with java bean object in session
Pablo Kang
pkang at laszlosystems.com
Thu Jun 30 16:22:13 PDT 2005
Hi Dzafer,
><javarpc name="jrpcUtil" classname="be.dzafer.handlers.Util_HDLR"
> scope="session" autoload="true" loadoption="loadcreate"
> objectreturntype="javabean">
>
>
>After the timeout session, I receive this message error: "could not find
>session instance jrpcUtil"
>So the loadoption="loadcreate" doesn't create a new javarpc ?
>
>
If your app is still up while the server session times out, invoking a
javarpc method will result with that message being returned.
>Is there a way to recreate when session lost?
>
>
If you get that message, you can try calling your javarpc's load()
method to recreate the server object, like:
jrpcUtil.load()
Of course, this assumes that the javarpc's loadoption is "loadcreate".
>Is there a way to retrieve an error code instead of a error message to know
>what kind of error has occurred?
>
>
There aren't any error codes returned today. You can try adding a
feature request on OpenLaszlo JIRA.
>So what I want to do:
>
>1) Create a bean and put it in session (for example: a javabean that
>contains all information about the logged user. Another example: keep in
>session a list of all connected user)
>2) Retrieve the bean into my Laszlo page for displaying information and also
>modifying information and put it back into session
>
>So, in one word sharing/modifying information between java
>Servlets/javabeans and the lzx pages.
>
>
JavaRPC sets its server session objects in an map that's keyed off
"__lzobj". So to set a session object on the server, you first must get
the map like:
Map map = req.getSession().getAttribute("__lzobj");
and then set your object like:
map.put("attutil", util_MOD)
You should then be able to retrieve the java from your LZX application:
<javarpc name="jrpcAttutil" attributename="attutil" classname="be.dzafer.models.Util_MOD"
scope="session" autoload="false" loadoption="loadcreate" objectreturntype="javabean">
....
</javarpc>
Note that there's a bug with loadoption="loadonly" so make sure you
specify "loadcreate" or you'll hit a null pointer exception error. I
have a fix in my local branch that will should hopefully go in within
the next week or so.
I've attached a couple of test files. Compile the java files and place
them in your WEB-INF/classes/examples directory. The sessionjavabean.lzx
file basically does what your login() call does and myjavabean.lzx gets
a prototype of the javabean placed in the session called in
sessionjavabean.lzx.
Let me know how it goes.
pablo
>What have try:
>
>1) Sessionrpc: without success with JavaBeans! Works fine with "String"
>types.
>
>2) Javarpc :
>
> A)In a lzx page, the javarpc will instantiated my javabean
>"Util_HDLR"
>
> <javarpc name="jrpcUtil" classname="be.dzafer.handlers.Util_HDLR"
> scope="session" autoload="true" loadoption="loadcreate"
> objectreturntype="javabean">
> ...
>
>
> B) In the Java Bean I do this :
>
> public boolean login(String login, String password,
>HttpServletRequest req) {
>
> boolean logged = false;
>
> Util_MOD util_MOD = util_FAC.getUser(login,
>passenc);
>
> if (util_MOD != null) {
>
>req.getSession().setAttribute("attutil",util_MOD);
> } else {
> ... error ...
> }
> ...
>
> C) In another lzx page I do this to retrieve the info:
>
> <javarpc name="jrpcAttutil" attributename="attutil"
> classname="be.dzafer.models.Util_MOD"
> scope="session" autoload="false"
>loadoption="loadonly"
> objectreturntype="javabean">
>
> <method event="onerror" args="err">
> debug.write("errors: "+ err);
> </method>
>
> <method event="onload">
> debug.write("onload ...");
> jrpcAttutil.rcutillogin.invoke();
> </method>
>
>
> <remotecall name="rcutillogin"
>funcname="getUtil_s_login">
>
> <method event="ondata" args="bean">
> Debug.write("rcutillogin ondata:",
>data);
> </method>
> </remotecall>
> </javarpc>
>
>
> ...
>
> <button text="getBean"
> onclick="jrpcAttutil.load();" />
>
>
> D) error message in debugger : errors: couldn't find session
>instance attutil
>
>
>
>
>So, I really have trouble to share objects between java and Laszlo.
>It's really critical for my project to be able to do that.
>
>Is there two different sessions, one for the flash client and one for my
>servlets ?
>
>Thanks in advance for your help.
>
>Best regards,
>Dzafer
>
>
>
>
>============================================================================
>============================================================================
>
>
>-----Original Message-----
>From: Pablo Kang [mailto:pkang at laszlosystems.com]
>Sent: Friday, June 24, 2005 8:22 PM
>To: dzafer
>Cc: laszlo-dev at openlaszlo.org
>Subject: Re: [Laszlo-dev] sessionrpc with java bean object in session
>
>Is idDset empty after ondata? In the debugger, can you type
>idDset.childNodes[0] and tell me what you see?
>
>pablo
>
>dzafer wrote:
>
>
>
>>Hi all,
>>
>>Try to use "sessionrpc" to retrieve a java bean object in session.
>>
>>---------------------------------------------
>>java:
>>
>>class: be.dzafer.models.Util_MOD
>>
>>.
>>req.getSession ().setAttribute ("attUtil", util_MOD);
>>---------------------------------------------
>>
>>
>>---------------------------------------------
>>Lzx:
>>
>><dataset name="idDset" />
>>
>><sessionrpc id="sessmenu" autoload="true">
>>
>><remotecall name="getAttUtil" funcname="getAttribute"
>>dataobject="idDset" >
>><param value="'attUtil'" />
>>
>><method event="ondata" args="data">
>>Debug.write("default ondata:", data);
>></method>
>>
>></remotecall>
>>
>></sessionrpc>
>>---------------------------------------------
>>
>>---------------------------------------------
>>Result:
>>
>>default ondata: <object#0| {class: be.dzafer.models.Util_MOD}>
>>---------------------------------------------
>>
>>
>>How can I manipulate this java object to retrieve object attributes?
>>
>><text datapath="idDset:/item/name/text()" />
>><text datapath="idDset:/item" />
>><text datapath="idDset:/item [1]/name/text ()" />
>>Doesn't display any results.
>>
>>Have try to use a text object instead of a java bean object, it work's
>>fine!
>>
>>
>>
>>Help!!! Thanks
>>
>>Best regard's
>>Dzafer
>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>Laszlo-dev mailing list
>>Laszlo-dev at openlaszlo.org
>>http://www.openlaszlo.org/mailman/listinfo/laszlo-dev
>>
>>
>>
>>
>
>
>
>
-------------- next part --------------
/* ****************************************************************************
* MyJavaBean.java
* ****************************************************************************/
/* J_LZ_COPYRIGHT_BEGIN *******************************************************
* Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
* Use is subject to license terms. *
* J_LZ_COPYRIGHT_END *********************************************************/
package examples;
/**
* Class used in ReturnJavaBeanExample.java.
*/
public class MyJavaBean {
String name;
String city;
String state;
String secret;
public MyJavaBean(String name, String city, String state, String secret) {
this.name = name;
this.city = city;
this.state = state;
this.secret = secret;
}
public String getName() { return name; }
public String getCity() { return city; }
public String getState() { return state; }
}
-------------- next part --------------
/* ****************************************************************************
* SessionJavaBean.java
* ****************************************************************************/
/* J_LZ_COPYRIGHT_BEGIN *******************************************************
* Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
* Use is subject to license terms. *
* J_LZ_COPYRIGHT_END *********************************************************/
package examples;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* Class used in examples/javarpc/sessionjavabean.lzx.
*/
public class SessionJavaBean {
String name;
public static Map getSessionMap(HttpServletRequest req) {
// Assume that __lzobj already exists.
return (Map)req.getSession().getAttribute("__lzobj");
}
public void create(String name, HttpServletRequest req) {
this.name = name;
MyJavaBean mjb =
new MyJavaBean(name, "san mateo", "california", "toomanysecrets");
Map map = getSessionMap(req);
map.put("myattr", mjb);
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public MyJavaBean getMyJavaBean(HttpServletRequest req) {
Map map = getSessionMap(req);
return (MyJavaBean)map.get("myattr");
}
}
-------------- next part --------------
<canvas debug="true">
<debug x="10" y="10" width="450" height="350" />
<security>
<allow>
<pattern>examples\..*</pattern>
</allow>
</security>
<javarpc name="create" classname="examples.SessionJavaBean"
scope="session" autoload="true"
objectreturntype="javabean">
<method event="onload">
Debug.write('----- javarpc proxy -----');
Debug.inspect(this.proxy);
// create object on server
this.create.invoke(['laszlo']);
</method>
<remotecall funcname="create">
<method event="ondata">
Debug.write('Invoking getMyJavaBean remotecall...')
parent.getMyJavaBean.invoke();
</method>
</remotecall>
<remotecall funcname="getMyJavaBean">
<method event="ondata" args="d">
Debug.write('----- MyJavaBean -----');
Debug.inspect(d);
</method>
</remotecall>
</javarpc>
</canvas>
-------------- next part --------------
<canvas debug="true">
<debug x="10" y="10" width="450" height="350" />
<dataset name="jbdset" />
<security>
<allow>
<pattern>examples\..*</pattern>
</allow>
</security>
<javarpc name="myjavabean" classname="examples.MyJavaBean"
attributename="myattr" scope="session" autoload="true"
objectreturntype="javabean">
<method event="onload">
Debug.write('----- javarpc proxy -----');
Debug.inspect(this.proxy);
// create object on server
this.getName.invoke();
</method>
<remotecall funcname="getName" dataobject="jbdset">
<method event="ondata" args="d">
Debug.write('----- MyJavaBean.getName() -----');
Debug.inspect(d);
Debug.write(jbdset.serialize());
</method>
</remotecall>
</javarpc>
</canvas>
More information about the Laszlo-dev
mailing list