title: How to implement username availability using JSP
body: <p>In my application I have a username with a textbox, a checkbutton and a password. After entering the username into the textbox, if I click on check button it should search in the MySQL database for the username, if it is available it should display the message if not an other message should be displayed.</p>
  
  <p><img src="https://i.stack.imgur.com/4ztnD.png" alt="enter image description here"></p>
  
  <p>How do I do that with JSP?
  i tried the follwing code:</p>
  
  <pre><code>    &lt;form  method="post" name="frm_addUser" action="./adduserserver.jsp"&gt;&lt;br&gt;&lt;br&gt;
  &lt;table width="500px;" border="0" cellpadding="5" cellspacing="1" bgcolor="#f8f8ff" bordercolor="#333366" align="center"&gt;
  
  
  &lt;tr&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;font size="4"&gt;User ID&lt;/font&gt;&lt;/td&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;input name="userid" style="WIDTH: 200px"&gt;&lt;/td&gt;&lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;font size="4"&gt;&amp;nbsp;&lt;/font&gt;&lt;/td&gt;
  
  &lt;!--&lt;td&gt;&lt;input value="Check availability" onclick="" class="btn_checkavail" type="button"&gt;&lt;/td&gt;&lt;/tr&gt;--&gt;
  &lt;/td&gt;
  &lt;td&gt;
  &lt;input type="submit" value="check" name="check" 
  onclick="" /&gt;&lt;/td&gt;&lt;/tr&gt;     
  &lt;tr&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;font size="4"&gt;Pass Word &lt;/font&gt;&lt;/td&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;input name="password" type="password" style="WIDTH: 200px"&gt;&lt;/td&gt;&lt;/tr&gt;
  
  &lt;tr&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;font size="4"&gt;Confirm Password &lt;/font&gt;&lt;/td&gt;
  &lt;td bordercolor="Gainsboro"&gt;&lt;input name="confirmpassword" type="password" style="WIDTH: 200px"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;
  &lt;%
  try{
  String username=request.getParameter("username");
  
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","sumith");
      st=con.createStatement();
  
      sqlQuery="select distinct username from usernameexist where username='"+username+"'";
      rs=st.executeQuery(sqlQuery);
  int count=0;
  while(rs.next())
  {
  count++;
  }
  if(count&gt;0)
  {
              out.println("&lt;html&gt;");
                          out.println("&lt;head&gt;");
              out.println("&lt;title&gt;MeterDetailsPage&lt;/title&gt;");
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
                          out.println("&lt;table align='center' color='red'&gt;");
                          out.println("&lt;tr color='red'&gt;");
                          out.println("&lt;td &gt;&lt;font size=4 color=red &gt;username  Already Exist&lt;/font&gt;&lt;/td&gt;");
                          out.println("&lt;/tr&gt;");
                          out.println("&lt;/table&gt;");
                          out.println("&lt;/body&gt;");
                          out.println("&lt;/html&gt;");
  }
  
  else
  {
  
  if(username!=null )
  
  {
  
  if(!username.equals(""))
  
  {
  
              //st.executeUpdate("insert into usernameexist(username) values('"+username+"')");
              out.println("&lt;html&gt;");
                          out.println("&lt;head&gt;");
              out.println("&lt;title&gt;username&lt;/title&gt;");
              out.println("&lt;/head&gt;");
              out.println("&lt;body&gt;");
                          out.println("&lt;table align='center'&gt;");
                          out.println("&lt;tr&gt;");
                          out.println("&lt;td &gt;&lt;font size=4  color=green&gt;&lt;b&gt;available &lt;/b&gt;&lt;/font&gt;&lt;/td&gt;");
              out.println("&lt;/table&gt;");
                          out.println("&lt;/body&gt;");
                          out.println("&lt;/html&gt;");
  }
  }
  }
  st.close();
  con.close();
  }
  catch(Exception e){}
  %&gt;
  
  
  
  &lt;/table&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>