0

session in zk

asked 2011-07-27 04:41:21 +0800

pjayaprakash86 gravatar image pjayaprakash86
159 2

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

delete flag offensive retag edit

5 Replies

Sort by ยป oldest newest

answered 2011-07-28 00:27:30 +0800

gellaps gravatar image gellaps
157 1

updated 2011-07-28 00:28:17 +0800

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

link publish delete flag offensive edit

answered 2011-07-28 03:19:52 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Very good explanation gellaps, thanks

link publish delete flag offensive edit

answered 2011-07-28 04:52:45 +0800

pjayaprakash86 gravatar image pjayaprakash86
159 2

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");
}

link publish delete flag offensive edit

answered 2011-07-28 05:28:07 +0800

pjayaprakash86 gravatar image pjayaprakash86
159 2

also gellaps when i use User user=new User i am getting error.so please tell me which class to import

link publish delete flag offensive edit

answered 2011-07-28 22:05:09 +0800

gellaps gravatar image gellaps
157 1

updated 2011-07-28 22:10:24 +0800

@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.

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: 2011-07-27 04:41:21 +0800

Seen: 483 times

Last updated: Jul 28 '11

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