I have trawled Google over and over but I cannot find anything about storing a variable in the cache. Anything I have seen related to the matter looked very complicated and was not well explained. I am designing a very simple login system with GWT using a text-file that edits a users line by appending true or false depending on whether they are logged in or out respectively.
The problem is that when the user refreshes the window they are not logged out (because I don't want them to be) but the application has lost the current user variables so it appears that they are log开发者_开发问答ged out. I would like to cache the username of the current user until they logout so that I can allow the user to refresh/close the tab and still be logged in. I am looking for quite an in depth explanation as I have little/no previous experience with caching! If the question seems stupid or a solution is not possible please tell me!
Setting Cookies is way easier than described here! I'll just give you an example:
//set a cookie
final TextBox t1 = new TextBox();
RootPanel.get().add(t1);
final TextBox t2 = new TextBox();
RootPanel.get().add(t2);
Button b1 = new Button("safe cookie");
b1.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// a cookie which expires after one week
Date now = new Date();
long nowLong = now.getTime();
nowLong = nowLong + (1000 * 60 * 60 * 24 * 7);// seven days
now.setTime(nowLong);
Cookies.setCookie(t1.getText(), t2.getText(), now);
//if the time isn't set it expires after the tab closes
}
});
RootPanel.get().add(b1);
//get a cookie
final TextBox t3 = new TextBox();
RootPanel.get().add(t3);
Button b2 = new Button("get cookie by name");
b2.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert(Cookies.getCookie(t3.getText()));
}
});
RootPanel.get().add(b2);
Sources: Set Cookies with GWT applications to expire after expected time period, Class Cookies
HTML5 was the best and most simple solution that I could find.
I used the GWT Storage API which can be found here in Eclipse and it was extremely simple. If anyone else wants to do a similar thing everything is explained in the link I provided.
GWT does't provide any options for storing value in browser but you can use 'cookie' to store data on browser. GWT will help you to create and maintain cookie on browser. If you are planing to store more data on browser use HTML 5 features.
Google Gear with GWT is also an option to store data on browser
You could store the data inside the user session and fetch it from the server onModuleLoad() (or whenever you need it) via RCP. It is a bad idea from a security point of view to store the login state on the client because he could change it to "logged in".
精彩评论