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

  • Home
  • SEARCH
  • 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 994619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:36:11+00:00 2026-05-16T06:36:11+00:00

I want to create a login page using Servlet & JSP. I ve created

  • 0

I want to create a login page using Servlet & JSP.

I ve created a page which gets Username & password.

I made a database with a table which contains Username & password.

<form action="LoginPage" method="POST">
    User name: <input type="text" name="userName" size="20"><br>
    Password: <input type="password" name="password" size="20">
    <br><br>
    <input type="submit" value="Submit">
</form> 

I entered the below code in doPost()

 response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String userName = request.getParameter("userName").toString();
    String passWord = request.getParameter("password").toString();
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "userdb";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root"; 
    String password = "1234";
    try {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url+dbName, user, password);
        PreparedStatement pstmt;
        String sql = "SELECT USR_NAME FROM LOGIN WHERE USR_NAME='userName'";
        pstmt = conn.prepareStatement(sql);
        ResultSet rs=pstmt.executeQuery();
        String usr = null;
        String pass = null;
        while(rs.next())
        {
            pass = rs.getString(3);
        }
        if(pass != null && pass.equals(passWord))
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Login Sucessfull</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Login Sucessfull " + request.getContextPath () + "</h1>");
            out.println("<p>Welcome</p> " + userName);
            out.println("</body>");
            out.println("</html>");

            out.close();
        }

    } catch (Exception e) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Login is not Sucessfull</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Login is not Sucessfull " + request.getContextPath () + "</h1>");
        out.println("<p>Wrong Username Or Password</p> ");
        out.println("</body>");
        out.println("</html>");

        out.close();

And I dont know how to make it work.
Any Quick Fix Available For me?

Its not a big project, i jus want a Login page which gets username & password
then the servlet will search the username in DB then checks for password.
I have made the changes you guys told. But the output looks like something
wrong in try block, Because, I get the Login not successful page.
I put the code “Login not successful Page” in Catch block.

  • 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-05-16T06:36:11+00:00Added an answer on May 16, 2026 at 6:36 am

    It’s unclear what you mean with “How to make it work”. What happens? What happens not? At least I can spot several problems in your code:

    1. You are emitting HTML inside a servlet. You should use JSP for this.
    2. You are leaking JDBC resources. You need to close them in finally.
    3. You are not setting the entered username (and password) in the SQL string.
    4. You are not letting the DB do the task of comparing the password. Add it to the WHERE.
    5. You are swallowing the exception. All detailed info about the problem cause get lost. You should either rethrow it as ServletException or at least log the exception type, message and cause. This information is important since it tells something about the root cause of the problem. You know, once the root cause is understood, the solution is obvious.

    Further it’s also bad user experience if you change the page to a page where the user can do absolutely nothing else than facing an error message. The user has to take extra handling to go back to the login page to re-enter the details. Rather redisplay the same page with the error message inlined.

    Rewrite your doPost() method as follows:

    String userName = request.getParameter("userName");
    String passWord = request.getParameter("password");
    
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "userdb";
    String user = "root"; 
    String password = "1234";
    String sql = "SELECT * FROM LOGIN WHERE USR_NAME = ? AND USR_PASS = ?"; // Not sure how the password column is named, you need to check/update it. You should leave those ? there! Those are preparedstatement placeholders.
    
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    boolean login = false;
    
    try {
        Class.forName(driver); // You don't need to call it EVERYTIME btw. Once during application's startup is more than enough.
        connection = DriverManager.getConnection(url + dbName, user, password);
        statement = connection.prepareStatement(sql);
        statement.setString(1, userName);
        statement.setString(2, password);
        resultSet = statement.executeQuery();
        login = resultSet.next();
    } catch (Exception e) {
        throw new ServletException("Login failed", e);
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
    
    if (login) {
        request.getSession().setAttribute("username", userName); // I'd prefer the User object, which you get from DAO, but ala.
        response.sendRedirect("home.jsp"); // Redirect to home page.
    } else {
        request.setAttribute("message", "Unknown username/password, try again"); // This sets the ${message}
        request.getRequestDispatcher("login.jsp").forward(request, response); // Redisplay JSP.
    }
    

    And add ${message} to your JSP:

    <form action="LoginPage" method="POST">
        User name: <input type="text" name="userName" size="20"><br>
        Password: <input type="password" name="password" size="20">
        <br><br>
        <input type="submit" value="Submit"> ${message}
    </form>
    

    Here are some links to learn how to do JSP/Servlet/JDBC properly.

    • Beginning and Intermediate JSP/Servlet tutorials
    • Advanced JSP/Servlet tutorials (with JDBC)
    • DAO tutorial – How to create a proper data layer
    • DAO tutorial – Using in JSP/Servlet
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.