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 6668613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:03:36+00:00 2026-05-26T03:03:36+00:00

I have this Servlet code, I am trying to check for duplicate usernames in

  • 0

I have this Servlet code, I am trying to check for duplicate usernames in the database but it does not seem to work out.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String confirmpassword = request.getParameter("confirm_password");

    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/account";
        Connection conn = DriverManager.getConnection(url, "root", "school");

        Statement statement = (Statement) conn.createStatement();
        ResultSet rs = statement.executeQuery("SELECT * from Users where username='" + username + "';");

        String duplicate;

        while (rs.next()) {
            duplicate = rs.getString(username);

            if (password.equals("confirmpassword") && duplicate != username) {
                statement.executeUpdate("INSERT INTO info values('" + username + "','" + password + "');");
                out.println("Registraion Successful!");
                out.println("Your Username: "+username);
                out.println("Your Password: "+password);

            }
            if (duplicate.equals(username)){
                out.println("Please choose a different username..:)");
            }
        }




    } catch (SQLException ex) {
        Logger.getLogger(RegistrationServlet.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
    } finally {
        out.close();
    }
}
  • 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-26T03:03:37+00:00Added an answer on May 26, 2026 at 3:03 am

    The problem is the line duplicate = rs.getString(username);. You meant to retrieve the value in the username column from the result set, but you’re using the username variable, not the string "username". When you enter “ankur” as the username, that line is equivalent to duplicate = rs.getString("ankur");, and there’s no column named ankur in the result set.

    You have a bunch of other bugs as well:

    • rs.next() will only return true if the query found an existing user record with the specified username. That means you can only get to the successful registration code if there is a duplicate user.
    • password.equals("confirmpassword") means that the user has to actually type the word “confirmpassword” in the password box or their registration will be rejected. I think you meant password.equals(confirmpassword).
    • You’re logging into your database as root and you have SQL injection vulnerabilities. A malicious user could do anything they want with your database, such as modifying or deleting your data.
    • Why are you catching and ignoring ClassNotFoundException?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some code on production which is effectively doing what this code does:
I have this java servlet that grabs information from a form, I need to
I have tested this servlet and it works well, except in Google Chrome it
I have a servlet which takes us to an existing jsp, say home.jsp. This
I have this code in jQuery, that I want to reimplement with the prototype
Sorry about the vague title but I have a servlet with the following mapping
I am trying to do a simple application using Ajax and servlet. But when
I have an applet trying to send java serialized object to a servlet (hosted
I am trying to integrate the code from this demo: http://code.google.com/p/gwt-spring-starter-app/ into my main
I'm trying to use getServletContext().getRealPath( / ) , but I keep getting this error:

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.