Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Hi

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Hi

    Hello I am new to Java. My requirement is I have login and logout application.I am using cookies and session.If I don't logout and close the window, next time I should get page with my login.If I logout and close then login page.Plz help me in this regard.thanks in advance

  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Hi

    Hello and welcome to the forums, I think you will have to elaborate somewhat more on your problem please.

    // Json

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi

    Yes sure.I am using struts and jsp to make a login application.Just login and logout.I am using cookies and session.I want the application to be--if the user logs in and just closes the browser, without using the logout button, then if the browser is opened again, it show the previous login page(i.e the first page after login).If he closes using logout, no change(as usual it must come to login page to give username and password.)I think I am clear now.Any help is appreciated.

    Thank you

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Hi

    Well, basically there are two things you need to implement.

    1. Once the user logs in, store the user in a cookie somehow, for instance you could store the username and the hashed password + salt.

    2. In your method to check if a user is logged in to your site, grab the cookie data and if there is some data there, perform a normal login.

    Here is some code to get you going.

    final Cookie usernameCookie = new Cookie("username", userBean.getUsername());
    usernameCookie.setMaxAge(60 * 60 * 24 * 365); // 1 year expiry
     
    final Cookie passwordCookie = new Cookie("password", userBean.getPassword());
    passwordCookie.setMaxAge(60 * 60 * 24 * 365); // 1 year expiry
     
    response.addCookie(usernameCookie);
    response.addCookie(passwordCookie);

    Thats a snippet of code you can use to store the two cookies. userBean,getUsername() and getPassword() returns the username and the hashed password + salt. Then you call response.addCookie() to add these. Response is of type HttpServletResponse.

    Once this is done all you need to do is read the values and try a login once the user needs to be authenticated. The method below is a convenience method I use to scan through the cookies in the request and return the cookie with the name supplied.

        /**
         * Gets a cookie off the {@link HttpServletRequest} by name.
         *
         * @param request the {@link HttpServletRequest}
         * @param name the name of the cookie, this is case sensitive
         * @return the cookie if found otherwise null
         */
        public static Cookie getCookie(final HttpServletRequest request, final String name) {
            if (request != null && request.getCookies() != null && StringUtils.isNotBlank(name)) {
                for (final Cookie cookie : request.getCookies()) {
                    if (name.equals(cookie.getName())) {
                        return cookie;
                    }
                }
            }
     
            return null;
        }

    Now all we need to do is use this method to get the cookies off of the request and then perform the authentication check.

    final Cookie userCookie = getCookie(request, "username");
    final Cookie pwdCookie = getCookie(request, "password");
     
    if(userCookie != null && pwdCookie != null) {
        // Check the username userCookie.getValue() and the password pwdCookie.getValue() against your user persistence layer and off we go.
    }

    Thats about it.

    NOTE: Any references to StringUtils is in the apache commons lang library which can be found at Lang - Home

    // Json

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Hi

    Thank you....