1. First, introduce the jQuery package in the jsp file in the foreground, and write the path of the file and the settings of the text box
<script src="./js/jquery-1.7.min.js"></script>
<!-- Employee ID--> <div class="am-form-group"> <label for="user-name" class="am-u-sm-3 am-form-label">员工编号</label> <div class="am-u-sm-9"> <input type="text" name="no" style="width: 300px; float: left;" id="no" placeholder="Please enter employee number"><span style="color: #F00; float: left; margin-top: 5px;">* </span> <span id="vno" style="padding-top: 10px;"></span> </div> </div> <!-- Department-->
2. In the jsp file, write the code of jquery
<script> //Use ajax to determine whether the employee's number is registered $(function(){ $('#no').blur(function(){//Mouse move away event var no = $('#no').val();//Get the value in the text box /* alert(no); */ // part of the path var path = '<%=request.getContextPath()%>'; $.ajax({ type : "post",//How to submit url : path + "/employee",//full path async : false, data : {//The passed value and the method to jump to the servlet file "no" : no, "action" : "check" }, //If successful, determine whether the number is available success : function(data) {//The sql statement in the Dao file has already queried how many numbers are entered at the front desk, if 1 is unavailable, if 0 is available if (data > 0) { $('#vno').text('The username ID is repeated and cannot be used'); } else {//Determine whether the text box is empty if (no == "" || no == null) { $('#vno').text('Please enter the employee number'); } else { $('#vno').text('Congratulations! This user ID can be registered'); } } }, error : function() { alert("failure") } }); }) }) </script>
3. Write the method in the servlet file
else if (action != null && action.equals("check")) { /** * Determine whether the number is registered */ //define a print PrintWriter print = response.getWriter(); EmployeeDao employeedao = new EmployeeDao(); //Get the number in the foreground text box String no = request.getParameter("no"); try {//Pass the data returned in the Dao file to shu int shu = employeedao.bianhao(no); // Pass the information to the data in the foreground print.print(shu); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace (); } print.flush(); print.close(); }
4. Write the method called in the servlet method in the Dao file
/** * Determine whether the employee number has been registered */ public int bianhao(String no) throws SQLException { //Query the number of numbers written in the foreground in the database String sql = "select count(EM_NO) from task_employee where EM_NO=?"; Connection conn = null; PreparedStatement stmt = null; //define a count int count = 0; conn = getConn(); stmt = conn.prepareStatement(sql); stmt.setString(1, no); ResultSet rs = stmt.executeQuery(); //Get the number entered in the text box and store the number in the database into count if (rs.next()) { count = rs.getInt(1); } //Return the number of query IDs in the database return count; }
final style