Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:11:38+00:00 2026-06-15T09:11:38+00:00

I have a no-framework web application. I need to implement a simple way to

  • 0

I have a no-framework web application. I need to implement a simple way to check unsuccessful logins, using sessions. If the user attempts to log in 3 times using incorrect username/password combination, they will be given a 20 minute timeout before they can try logging in again.

Currently I only set a user session if the user successfully logs in to the system. However, it seems that I should get a session in case of unsuccessful login also, and count the login attempts somehow.

Login.jsp (simplified version):

<form name="loginForm" method="post" action="CustomerData">
User name:<input type="text" name="userName"/>
Password:<input type="password" name="password"/>
<input type="button" value="submit">

CustomerData.java (simplified version):

// See if customer is a valid user
        String selectQuery = "Select firstName,lastName,email from customer where userName='"+userName+"' and password='"+password+"'";
        selectResult = statement.executeQuery(selectQuery);

if(selectResult.next())
{
    // We got a valid user, let's log them in
    ....
    HttpSession session = request.getSession(true);
    session.setAttribute("customer", customer);
}
else
{
    // this is where I need to get the session id (??),
    // count the unsuccessful login attempts somehow, 
    //and give them a 20 minutes timeout before they can try logging in again.

    request.setAttribute("message","Invalid username or password. Please try again!");

}

While doing research, I found that there are a lot of built-in security features for various Java frameworks. I also found that using sessions is not the best way to track login attempts, because the user can log-in with different browsers. However, I’m creating this functionality for a simple web project that will never go to any production environment. I would like to know how to implement this functionality using the Java HTTPSession Object.

Ok, here is my full solution, based on the feedback I received. I’m posting this in case it might help others with similar issues:

// See if customer is a valid user
String selectQuery = "Select firstName,lastName,email from customer where userName='"+userName+"' and password='"+password+"'";
selectResult = statement.executeQuery(selectQuery);

        if(selectResult.next())
        {
            // We got a valid user, let's log them in
            Customer customer = new Customer();
            customer.setFirstName(selectResult.getString("firstName"));
            customer.setLastName(selectResult.getString("lastName"));
            customer.setEmail(selectResult.getString("email"));
            customer.setUserName(userName);
            customer.setPassword(password);

            // establish a user session
            session.setAttribute("customer", customer);
            session.setAttribute("firstName", customer.getFristName());
            url = "/index.jsp";
            selectResult.close();

        }
        else
        {
            int loginAttempt;
            if (session.getAttribute("loginCount") == null)
            {
                session.setAttribute("loginCount", 0);
                loginAttempt = 0;
            }
            else
            {
                 loginAttempt = (Integer) session.getAttribute("loginCount");
            }

            //this is 3 attempt counting from 0,1,2
            if (loginAttempt >= 2 )
            {        
                long lastAccessedTime = session.getLastAccessedTime();
                date = new Date();
                long currentTime = date.getTime();
                long timeDiff = currentTime - lastAccessedTime;
                // 20 minutes in milliseconds  
                if (timeDiff >= 1200000)
                {
                    //invalidate user session, so they can try again
                    session.invalidate();
                }
                else
                {
                     // Error message 
                     session.setAttribute("message","You have exceeded the 3 failed login attempt. Please try loggin in in 20 minutes, or call our customer service center at 1-800 555-1212.");
                }  

            }
            else
            {
                 loginAttempt++;
                 int allowLogin = 3-loginAttempt;
                 session.setAttribute("message","loginAttempt= "+loginAttempt+". Invalid username or password. You have "+allowLogin+" attempts remaining. Please try again! <br>Not a registered cusomer? Please <a href=\"register.jsp\">register</a>!");
            }
            session.setAttribute("loginCount",loginAttempt);
            url = "/login.jsp";

        }

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:11:39+00:00Added an answer on June 15, 2026 at 9:11 am

    You can try the below code

    int loginAttempt = (Integer)session.getAttribute("loginCount");
    
    if (loginAttempt > 3 ){
         // Error message/page redirection 
    }else{
         session.setAttribute("loginCount",loginAttempt++);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this web application using Spring Web Flow framework. In my main page
I have a j2ee web application running on Spring framework. I want to implement
I'm building a web application using PHP5.3 and Zend Framework 1.9.4. i have an
All, I have a PHP Web application built using Zend Framework and MVC with
I have a asp.net web application using MVC framework, the website allows the registrars
I need to develop a lightweight web application, it will: have a simple webgui
I have build a web application in Zend Framework and now desperately need a
I have the following scenario. I have created an ASP.NET web application (framework 3.5)
I have a web application that uses the Spring Framework (3.1) and persistence through
I have in my web application an ADO.NET Entity-Framework *.edmx file. When I browse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.