I am new to jquery and ajax. I want to check whether the given username is present in my DB or not if it is available I want to give an alert by passing a message called username already exist. I am trying to apply the blur method of jquery so before submitting the form I can get the status about that username. Please help me to solve this.
This is my JSP page
<body>
    <div class="container registration">
        <fieldset class="border p-2">
            <legend class="w-auto">Registration Form</legend>
            <form action="registration" method="post" id="registrationForm"
                onsubmit="return validation();">
                <div class="row" style="padding-bottom: 20px">
                    <div class="col">
                        <input type="text" class="form-control" name="firstName"
                            placeholder="First name" id="fname"> <span id="firstname"
                            class="text-danger font-weight-bold"></span>
                    </div>
                    <div class="col">
                        <input type="text" class="form-control" name="lastName"
                            placeholder="Last name" id="lname"> <span id="lastname"
                            class="text-danger font-weight-bold"></span>
                    </div>
                </div>
                <div class="row" style="padding-bottom: 20px">
                    <div class="col">
                        <input type="text" class="form-control" name="userName"
                            placeholder="UserName" id="uname"> <span id="username"
                            class="text-danger font-weight-thin"></span>
                    </div>
                    <div id="ajaxGetUserServletResponse"></div>
                </div>
                <!-- below jquery things triggered on onblur event and checks the username availability in the database -->
                <script type="text/javascript">
                    $(document).ready(function() {
                        alert("js is working");
                        $('#uname').blur(function() {
                            alert("blur is working")
                            $.ajax({
                                    type : 'post',
                                    url : 'registration',
                                    data : {
                                                uname : $('#uname').val()
                                            },
                                            success : function(responseText) {
                                                $('#ajaxGetUserServletResponse').text(responseText);
                                            }
                                    });
                            });
                    }); 
                </script>
This is my servlet
@WebServlet("/registration")
public class RegistrationServlet extends HttpServlet {
    Registration userDetails = Utility.getRegistration();
    // RegistrationService registrationService = Utility.getRegistrationService();
    UserDetailsService userDetailsService = Utility.getUserService();
    JSONArray array = null;
    JSONObject jsonObject;
    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fName = req.getParameter("firstName");
        String lName = req.getParameter("lastName");
        String uName = req.getParameter("userName");
        String email = req.getParameter("email");
        String contactNumber = req.getParameter("contactNumber");
        String password = req.getParameter("password");
        userDetails.setFirstName(fName);
        userDetails.setLastName(lName);
        userDetails.setUserName(uName);
        userDetails.setEmail(email);
        userDetails.setMobile(contactNumber);
        userDetails.setPassword(password);
        try {
            jsonObject = UserDetailsRepository.getOneUserDetails(uName);
            if (jsonObject != null) {
                String msg = "User is already exist";
                resp.setContentType("text/plain");
                resp.getWriter().write(msg);
            //  throw new UserAlreadyExistException("User is already exist");
            } else {
                boolean result = userDetailsService.addUser(userDetails);
                if (result) {
                    req.setAttribute("insert", "Data is added into the table successfully!!!");
                    resp.sendRedirect("index.jsp");
                } else {
                    req.setAttribute("message", "Something went wrong!!!");
                    resp.sendRedirect("error.jsp");
                }
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
 
    