Question

For my web application i have a text field for username i need to check username availability from mysql database and should display status below the text field as available or unavailable..

i have changed my code like this its partially working So pls go through this code and help me....

This is my JSP:

<tr>
<td>Choose your UserName* :</td>
<td><script type="text/javascript" src="jquery.js"></script>
<input type="text" name="txtUsername" id="username">@gmail.com
<div id="status"></div>
<script type="text/javascript" src="js/check_user.js"></script>
<span id="errorMissingUserName" style="display:none;"><font color="red">*Please provide your username.</font></span>
<span id="errorUserNameInvalid" style="display:none;"><font color="red">*Please provide a valid username.Username can contain only alphabets numbers and periods</font></span>
<span class="status"></span>
</tr>

And this is my servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter out=response.getWriter();
            String uname=request.getParameter("txtUsername");
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname/uname/pass");
                PreparedStatement ps=conn.prepareStatement("select * from register where UserName=?");
ps.setString(1,uname);
                ResultSet rs=ps.executeQuery();
                if(rs.next()){
                    out.println("<font color=red>");
                    out.println("UserName not available");
                    out.println("</font>");
                    }
                else{
                    out.println("<font color=green>");
                    out.println("UserName available");
                    out.println("</font>");
                }
                rs.close();
                ps.close();
                conn.close();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

Only else clause is working and if clause is not working

And this is my check_user.js

$('#username').keyup(function()

{
var username=$('#username').val();
$('#status').html('<img src="images/username_loader.gif" >');
if (username!=''){
    $.post('CheckUsername',{username:username},
            function(data)
            {
        $('#status').html(data);
            });

}
else{
    $('#status').html('');
    }
});

I realized that String uname = request.getParameter("txtUsername"); is giving username a null value to the servlet... How to check when each letter is typed to the jsp and compare it with the database name already present?? Some one please help me solve this....

Was it helpful?

Solution

pass "id" instead of "name" in the getParameter as

 String uname=request.getParameter("username");

do this it will work.

OTHER TIPS

Apparently you are submitting the username under the key 'username' and you are trying to retrieve it using the key 'txtUsername'. That won't work of course.

(moved from comment to an answer on request)

Your if and else are reversed. You're saying: if I find the username in the register, then it's available and if I don't find the username in the register then it's unavailable. Don't you mean the opposite. If it's not already in the register, then it should be available.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top