会话管理GWT [英] Session management in gwt

查看:121
本文介绍了会话管理GWT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用GWT为我的客户端应用程序。但是,我不知道我怎么能处理的会话管理。在GWT应用程序驻留在一个页面上,所有的服务器调用通过AJAX实现。如果一个会话过期的服务器上。让我们假设用户没有关闭浏览器,并使用RPC发送一些请求到服务器,怎么可能我的服务器通知会话已过期,并且客户端侧部分应该再次显示登录界面的应用程序吗?我的样品code:

I am using GWT for my client side application. However, I am not sure how I can handle session management. The GWT application resides on one page, all server calls are done via AJAX. If a session expires on the server. let's assume the user didn't close the browser, and sending some request to server using RPC, how could my server notify the application that the session has expired and that the client side portion should show the login screen again?My sample code :

ContactDataServiceAsync contactDataService = GWT
				.create(ContactDataService.class);
		((ServiceDefTarget) contactDataService).setServiceEntryPoint(GWT
				.getModuleBaseURL()
				+ "contactDatas");

		contactDataService.getContact(2,
				new AsyncCallback<ContactData>() {
					public void onFailure(Throwable caught) {
                                      //code to show error if problem in connection or redirect  to login page

					}

					public void onSuccess(ContactData result) {
						displayContact(result);
					}
				});

如果会议只是过期它显示登录界面,否则要展示使用Window.alert()。有些错误

If session expires only it has to show login screen, otherwise it wants to show some error using Window.alert().

如何做到这一点,什么是所有codeS需要在服务器端和客户端?

How to do this and what are all the codes needed in server side and client side?

推荐答案

您可以在服务器抛出的AuthenticationException在情况下,用户已经被注销的客户端。
这将被钓到的回调onFailure方法,然后可以将用户重定向到登录页面。

You could have the server throw an AuthenticationException to the client in case the user has been logged out.
This will be catched in the callbacks onFailure method, which then can redirect the user to the login-page.

编辑:
的AuthenticationException当然不是一个标准的异常,我只是做一个例子。这可能是最好坚持使用标准的例外。


AuthenticationException is not a standard exception of course, i was just making an example. It might be best to stick with the standard exceptions.

要尝试,如果你发现你可以使用instanceof运算符
一个特定的异常

To try if you caught an specific exception you could use the instanceof operator

	public void onFailure(Throwable e) {
                  if(e instanceof AuthenticationException) {
                        redirecttoLogin();
                  }
                  else {
                    showError(),
               }
            }

这篇关于会话管理GWT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆