0

Refresh Desktop Page frm servlet in same sess

asked 2007-09-19 10:03:49 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4526231

By: vidhyau

Problem:

a. In a browser I open up a zul page.
b. And in another browser, I call a servlet (in the same session with jsessionid) and the desktop id.
c. All I would need the servlet to do is redirect/refresh the page of the desktop in the 1st browser.

I was able to get HttpSession, simplesession ,desktop from the cache, and the corresponding page that need to be refreshed.

But, I do not know how to redirect it to the same page(i.e, refresh) in the same browser

when i as UIEngine().sendRedirect. It says, this can be executed only through Event Listeners. Is there a way out?

Thanks!!!


delete flag offensive retag edit

3 Replies

Sort by ยป oldest newest

answered 2007-09-20 04:34:38 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4527983

By: waterbottle

Hi,
Since HTTP is request base, so, you can not change any UI if not in corresponding request(event).
How ever in ZK 3.0 RC , provides a new feature "Server Push".
Use Server Push, you can change UI even in thread or another deskotp or session.
Below is example of change deskotp(listner.zul) to another url by another desktop in same session (sender.zul)

1.First create a thread to check redirect command (url in session).
==
package serverpush;
import org.zkoss.lang.Threads;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zul.Label;
public class CheckRedirectionThread extends Thread {
private Desktop _desktop;
private Session _session;

private int _cnt = 0;

private boolean _ceased;

public CheckRedirectionThread(Desktop desktop) {
_desktop = desktop;
_session = _desktop.getSession();
}
public void run() {
try {
while (!_ceased) {
System.out.println("==>running:"+_cnt);
Threads.sleep(1000); // Check redirection every second
boolean r = Executions.activate(_desktop,1000); // get full control of desktop
if(!r) continue;
try {
String uri = (String)_session.getAttribute("redirectTo");
if(uri!=null){
_session.removeAttribute("redirectTo");
Executions.sendRedirect(uri);
setDone();//done and break.
}
} catch (RuntimeException ex) {
throw ex;
} catch (Error ex) {
throw ex;
} finally {
Executions.deactivate(_desktop); // release full control of
}
_cnt++;
}
}catch (IllegalStateException ex){
ex.printStackTrace();
}catch (InterruptedException ex) {
ex.printStackTrace();
}finally{
_session = null;
_desktop = null;
}
System.out.println("stop thread:"+_cnt);
}
public void setDone(){
_ceased = true;
}
}
==
2.In listener.zul, start the Server Push of this desktop, and new a CheckRedirectionThread to handle redirection command (url in session).
==
<window title="Demo of Server Push - listener">
<zscript>
serverpush.CheckRedirectionThread thread;

void startServerpush(){
desktop.enableServerPush(true);
lab.value = "lintening";
thread= new serverpush.CheckRedirectionThread(desktop);
thread.start();

}
void stopServerpush(){
thread.setDone();
desktop.enableServerPush(false);
thread = null;
lab.value = "stoped";
}
</zscript>
<label id="lab" value="stoped"/>
<vbox>
<button label="Start listen redirect" onClick="startServerpush()"/>
<button label="Stop listen redirect" onClick="stopServerpush()"/>
</vbox>
</window>
==
3.In sender.zul, just set new url to session as a reidrection command.
So, you can do same thing in your servlet.
==
<window title="Demo of Server Push - sender">
<zscript>
void sendRedirect(){
session.setAttribute("redirectTo",uri.value);
}
void clearSendRedirect(){
session.removeAttribute("redirectTo");
}
</zscript>
<vbox>
<textbox id="uri" value="http://www.zkoss.org"/>
<button label="Send Redirect" onClick="sendRedirect()"/>
<button label="Clear Send Redirect" onClick="clearSendRedirect()"/>
</vbox>
</window>
==

Run step:
1.use firefox, connect to listener.zul, click the "Start listen redirect"
button
2.open a tab (same session), connect to sender.zul, click the "Send Redirect"
button
3.the tab of listener.zul should change to http://www.zkoss.org

This is just for demo, you should take care more about thread stop and clean resource issue if use just reload/leave a page.

/Dennis

link publish delete flag offensive edit

answered 2007-09-23 16:39:32 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4532973

By: vidhyau

When tried this code with IE, it refreshes the Sender.zul instead of Listener.zul I guess.. thats because the desktop of the sender.zul is been handled by the Thread instead of the desktop of listener.zul


listener.zul
--------------


void startServerpush(){
desktop.enableServerPush(true);
lab.value = "lintening";
thread= new serverpush.CheckRedirectionThread(desktop);
thread.start();

}



link publish delete flag offensive edit

answered 2007-09-23 17:05:03 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4532987

By: vidhyau

Thanks!!!

Refreshing a desktop for another servlet worked..



Servlet Code:
-------------
protected void doGet(HttpServletRequest request, HttpServletResponse resp)
throws ServletException, IOException {

String desk = request.getParameter("desktop");
WebManager manager = WebManager.getWebManager(getServletContext());
SimpleSession session = (SimpleSession) manager.getSession(
getServletContext(), request);
SimpleWebApp webapp = (SimpleWebApp) manager.getWebApp();
Desktop desktop = webapp.getDesktopCache(session).getDesktop(desk);

try {
Executions.activate(desktop);
Executions.sendRedirect("/textbox.zul");
} catch (DesktopUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
Executions.deactivate(desktop);
}

}

zul for the 1st browseres
-------

<zk>
<zscript>
desktop.enableServerPush(true);
</zscript>
</zk>


request in another window for the servlet
-----------------------------------------
http://localhost:8080/servlet/myservlet;jsessionid=14B5B0830353C9A64B6651D1E660A
CDA?desktop=g3nq1


The 1st browser gets refreshed appropriately..


Thanks again!!!


link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2007-09-19 10:03:49 +0800

Seen: 777 times

Last updated: Sep 23 '07

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More