-4

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....

user3222718
  • 242
  • 1
  • 7
  • 27
  • Define not working and describe the problem you are having. There are too many problems in your code. – Bart Mar 04 '14 at 05:32
  • @Bart am new to javascript and am sure about my servlet.... So any problem should be in the javascript only and the < span > tag only... Any help would be appreciated... – user3222718 Mar 04 '14 at 05:35
  • You still didn't defined **not working** – Bart Mar 04 '14 at 05:50
  • Do you see any output when you visit the url in the browser? If the code throws an exception nothing is printed. – Bart Mar 04 '14 at 05:53
  • @Bart no i don see any output – user3222718 Mar 04 '14 at 05:57
  • Then check the stacktrace of the exception being thrown. – Bart Mar 04 '14 at 05:58
  • There is no exception being thrown... – user3222718 Mar 04 '14 at 05:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48920/discussion-between-bart-and-user3222718) – Bart Mar 04 '14 at 06:05
  • Hello Some one please help me on this struggling from a long time on this.... I have kept on editing the post... I have tried a lot solving this and i don't see any mistakes after editing but the problem still persists – user3222718 Mar 05 '14 at 07:39

3 Answers3

1

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)

Gimby
  • 5,095
  • 2
  • 35
  • 47
1

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

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

do this it will work.

user3349720
  • 179
  • 2
  • 12
0

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.

mamboking
  • 4,559
  • 23
  • 27
  • no if i change the other way round also only one condition is working the problem in the code is uname=request.getParameter (txtUsername); is getting a null value dono y – user3222718 Mar 06 '14 at 05:09
  • 1
    Well 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. – Gimby Mar 06 '14 at 09:35
  • @Gimby thanks a lot it worked i dono how i overlooked it... thanks a ton can u post this as an answer??? so that i can award you your bounty?? – user3222718 Mar 06 '14 at 09:46
  • As you wish, it is done. – Gimby Mar 06 '14 at 09:49