Results 1 to 2 of 2

Thread: Getting Username and Password with New java.io.Console Class

  1. #1
    Join Date
    Dec 2008
    Posts
    28

    Getting Username and Password with New java.io.Console Class

    hi
    i am trying to become familiar with the Console , as i am working on java.i am trying to Getting Username and Password with console.any one can help me little to find something??
    thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    3,026

    Re: Getting Username and Password with New java.io.Console Class

    hi
    you loking for some interesting thing check this .
    Getting a user’s username and password is probably one of the more common uses of the Console class. It’s fairly simple to do, but there are some things to look out for. Before you do anything else, you have to get a Console instance. Looking at the API, you’ll notice that Console has no public constructors. In fact, the only way to get a Console instance is by calling the System method:

    Console con = System.console();

    Simple enough, right? There is something you need to watch out for, however: The System.console() method can return null. According to the API:

    If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

    So, all you have to do is start it interactively, right? Well, sort of. This is the way it should work, but currently Eclipse (and some other IDEs) don’t yet support this feature “properly”, so if you try to run a program that calls System.console() from within your favorite IDE, it may return null. You have been warned.

    The following code demonstrates one method for getting login information.


    The first thing here is getting a Console instance and checking to make sure it’s not null before proceeding. Once you have a Console instance, you can use the readLine and readPassword methods to get the login information. Both of these methods are overloaded, and include the following overloads:



    The versions you use must combine prompting the user and getting input into one. If you really wanted to separate the two, you could use the Console.printf method to output your prompt and one of the no-arg methods above to get user input. That’s not necessarily incorrect, but definitely more verbose.

    The readLine method takes a formatted String and a variable list of arguments; exactly like the System.out.printf method. The readLine method returns a line of input from the console, as a String, not including line termination characters. It returns null, if the end of the stream has been reached. The readPassword method is almost identical in functionality, except that it returns a char array of the user input. You’ll also notice that it disables console echo, so someone walking by can’t see what’s being typed.

    Once you have the login information, using a method called authenticate to check it. The implementation of this method is outside of the scope of this post. You could stub it out and test it, using hard-coded values.

    as you done using the password information, you blank it out using the Arrays.fill utility method. This will minimize the lifetime of sensitive (password) data in memory.

    Incidentally, also wrapped all of this in a loop that will run until the authenticate method returns true or until count is equal to MAX_LOGINS.

    The Console class also has a few other methods, but the above four are the most interesting and likely to be the most-used. You might also use either the printf or format methods. According to the spec, these methods both behave identically to each other and exactly like System.out.printf. If you want to get the PrintWriter associated with this console, you can use the writer() method. Similarly, the reader() method retrieves the Reader object. Last but not least is the flush() method, which is a must since Console implements Flushable. Nothing surprising or groundbreaking with these last few methods.

Similar Threads

  1. Need help for getting the DSL username and password
    By Road-Runner in forum Networking & Security
    Replies: 4
    Last Post: 03-12-2010, 05:18 PM
  2. Converting java class to java bean class
    By Ucchal in forum Software Development
    Replies: 6
    Last Post: 09-08-2010, 10:24 AM
  3. How to read user input from console using Scanner class in java?
    By Constantinee in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 06:47 PM
  4. How to use Console class to read user input in java?
    By MABON in forum Software Development
    Replies: 4
    Last Post: 04-02-2010, 10:17 PM
  5. Replies: 7
    Last Post: 04-02-2008, 09:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,819,807.55982 seconds with 16 queries