-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I have an login screen,and some other pages .Now i am able to login and redirect to pages ,i need help in maintaining session and passing values to other pages in my application.I also need code for not allowing user to go back to login screen once logged in.should give warning message like page expired.
How to do this.
Thanks,
JP
if using JDBC
Set session
login.zul
String query = "SELECT ID, USERNAME, PASSWORD FROM USER_TBL where USERNAME = '"+username.getText()+"' AND PASSWORD ='"+password.gettext()+"'"; Statement st = conDB.createStatement(); ResultSet rs = st.executeQuery(query); if (rs.next()) { User user = new User(); user.setUserId(rs.getInt("ID")); user.setUserName(rs.getString("USERNAME")); user.setPassword(rs.getString("PASSWORD")); //session session.setAttribute("User", user); Executions.sendRedirect("index.zul");
catch session
index.zul
if (session.getAttribute("User") == null) { Executions.sendRedirect("login.zul"); }
not allowing user to go back to login screen
login.zul
if (session.getAttribute("User") != null) { Executions.sendRedirect("index.zul"); }
if using hibernate
Set session
login.zul
if(dao.findBy(" user.username = '"+username.getText()+"' AND user.password ='"+password.getText()+"'" ).size() > 0){ User user = (User) dao.findBy(" user.username = '"+username.getText()+"' AND user.password ='"+password=.getText()+"'" ).get(0); session.setAttribute("User", user); session.setAttribute("sessionFactory", sessionFactory); Executions.sendRedirect("/index.zul"); }
catch session
index.zul
if(session.getAttribute("User") == null){ Executions.sendRedirect("login.zul"); }else{ com.User users = (com.User) session.getAttribute("User"); label.setValue(users.getFname()); }
not allowing user to go back to login screen
login.zul
if (session.getAttribute("User") != null) { Executions.sendRedirect("index.zul"); }
i think, you can search them in Java forums
Very good explanation gellaps, thanks
Thank you so much gellaps.
I understood everything but where to put the code you gave for clicking the back button.
can u please specify where in index.zul i have to puthis code and also in login .zul
if (session.getAttribute("User") == null) {
Executions.sendRedirect("login.zul");
}
login.zul
if (session.getAttribute("User") != null) {
Executions.sendRedirect("index.zul");
}
also gellaps when i use User user=new User i am getting error.so please tell me which class to import
@pjayaprakash86
well, you cn put them on your zul or your composer class..
this is some example
for verifying sesssion in zul,
<!-- We have a window called "win", "win" have an Event Listener "onCreate", "onCreate" calling the "validate()" method. so when the "win" is created. the "validate()" method is called. --> <zk> <zscript> void validate() { if (session.getAttribute("User") == null) { Executions.sendRedirect("login.zul"); } } </zscript> <window id="win" border="normal" onCreate="validate()> <!-- window's content here --> </window> </zk>
for verifying sesssion in composer class,
the .java / composer class ComposerClass /* We have a composer class, must extends GenericForwardComposer, */ public void onCreate$win(Event event) { if (session.getAttribute("User") == null) { Executions.sendRedirect("login.zul"); } } the zul <window apply="com.package.composer.ComposerClass" id="win"> </window>
you can do the same for
//login.zul if (session.getAttribute("User") != null) { Executions.sendRedirect("index.zul"); }
Of course you will occuring some errors,
because the "User" is my class. you should create your own class.
Asked: 2011-07-27 04:41:21 +0800
Seen: 483 times
Last updated: Jul 28 '11