Solved

Login Question

Posted on 2004-08-13
9
228 Views
Last Modified: 2012-05-05
I have an application that I need a user to first log into an application before actually launching the application.  So, I want to provide a login window, which provides the ability to login to the server, or create a new account to log into the server.  Both the login and create account windows are separate windows.

Previously, I had both windows implemented as JDialogs - so, this way, I coudl have a "while !loggedIn" loop, since execution would stop until the dialogs returned.  But, I need to change these into JFrames now.  The problem is, of course, execution no longer waits for the modal JDialog to return, so I would get an infinite loop.  The way I got around this was to have two separate methods in my main application class that would open each different window whenever needed.  So, my final code in the constructor was:

doLogin();

// launch application - at this point we are gauranteed to have been connected to the server

my doLogin() would simple call the method to open the login window - the login window has a button that allows the user to close the login window and open the create account window.  If the user cancels creating an account, the login window will be reopened.  Of course, the problem here is that the "doLogin();" call would be passed by since it is not waiting for anything and the application will begin launching as the login screen is open.

My ugly hack around this for now is to set a flag in the main class - called loggedIn - that is initially false.  If the login or create account windows logs the user in, the flag is updated and the application can then be launched.  So, my code looks like:

doLogin();

// here comes the nasty part!
while ( !loggedIn ) { }

// launch application

What is a better way of getting around this?  Sorry for the long explanation!!!
0
Comment
Question by:ctjoumas
9 Comments
 
LVL 6

Expert Comment

by:expertmb
ID: 11792003
>>If the user cancels creating an account, the login window will be reopened.  Of course, the problem here is that the "doLogin();" call would be passed by since it is not waiting for anything and the application will begin launching as the login screen is open.

when user cancels the account creation then do System.exit(0);

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11792049
Personally i'd be more inclined to use the same window with say a CardLayout. One you've logged in, it moves to the next 'screen'
0
 

Author Comment

by:ctjoumas
ID: 11793258
I think that makes sense for the login and create account windows, but I'm not so sure about the main application.  This is a chat application, so using a CardLayout doesn't seem that it would work too well - from what I gather, all panels in a CardLayout will be the same size.  Once the user logs in, I am displaying a separate "tree" window...much like AIM.
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 100 total points
ID: 11794183

Hi,

Simple solution is to use thread/object synchronization, rather than the spinning loop...
Change the code to something like the following:

// Objectwide attribute.
Object loginWaitObject = new Object();

//
doLogin();

// Still need the boolean, just in case the user is REALLY fast, and gets it done between doLogin above and the wait() below!
while ( !loggedIn ) {
    synchronized( loginWaitObject ) {
        // wait without consuming CPU resources...
        loginWaitObject.wait();
    }
}

// launch application

Now, the method that sets the "loggedIn" boolean to true has to change to something like the following:

public void informLoggedIn() {
    loggedIn = true;
    synchronized( loginWaitObject ) {
        loginWaitObject.notify();
    }
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ctjoumas
ID: 11794548
Oh, and another reason I don't want to keep the tree visible is because the things being displayed on the tree are retrieved from the server.  So, I cannot setup the tree until a connection to the server has been made.

I somehow need to wait for the logging in process to finish before proceeding (other than using the ugly hack I have).
0
 

Author Comment

by:ctjoumas
ID: 11794558
cjjclifford:  I will take a look at that.  Sorry, I was in the middle of posting then got tied up for a few hours and finished posting before checking if anyone had responded.  I'll let you know what happens :)
0
 

Author Comment

by:ctjoumas
ID: 11794642
cjjclifford: Hmm..that works.  I'm not quite sure what is really going on here though, can you explain how that works?
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11794753

the "wait()" method basically suspends the current thread, until the "notify()" method is called on the object. When the "notify()" method is called, the suspended thread is woken up and the "wait()" call returns.
The "synchronized(obj)" code is used to become "the owner of this object's monitor" - see JavaDoc for "java.lang.Object" "wait" and "notify" for more details - not that if there is a possibility that multiple calls to wait() can be made for a single lock object, the "notifyAll()" method should be used instead (notify() only awakens a single wait() while notifyAll() awakens all wait() methods... generally for safety, notifyAll() can be used...

Hope this is description is helpful - its late Friday evening where I am, and I'm heading off for the weekend!
0
 

Author Comment

by:ctjoumas
ID: 11795386
Thanks a lot cjjclifford.  Your descrition is helpful...I read up on java.lang.Object wait and notify stuff earlier.  It made sense to me.  Your clarification of what synchronized does (becoming the owner of the object's monitor) explained the rest of it.  Everything is working...Thanks!

CEHJ - thanks for the tip about CardLayout.  I am using that for my login and create account screens and it is much simpler and more clean.  I haven't used that before and I'm glad you brought that to my attention :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Non-recursive backtracking, using a stack 1 103
rhino JavaScript import, load 25 69
even odd program using while loop 3 29
table example 4 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now