title: Servlet is not getting called by using jquery ajax in JSP?
body: <p>I am working with <code>JSP</code> and <code>Ajax</code> for the first time. I am trying to get one column data from database and populate it in my drop down list in my JSP page using Ajax call. I don't want to refresh the page so that is the reason, I am making an Ajax call.</p>
  
  <p>Here is my <a href="http://jsfiddle.net/4Nmqk/104/" rel="nofollow">jsfiddle</a> which has Process button and as soon as I click Process button, it will show an empty drop down list as of now. This is in my another <code>test.jsp</code> page.</p>
  
  <p>I have a table as <code>account</code> and I need to make this select query from the jsp -</p>
  
  <pre><code>SELECT USERS FROM ACCOUNT;
  </code></pre>
  
  <p>As soon as I am clicking Process button, I need to execute above SQL query on my POSTGRESQL database using Ajax. And whatever users, I am getting back from the database, I need to populate those <code>USERS</code> in my drop down list as shown in my above jsfiddle. </p>
  
  <p>Below is my servlet code which I am calling from a JSP page using Ajax.</p>
  
  <pre><code>@WebServlet("/someservlet/*")
  public class SomeServlet {
  
      public void doGet(HttpServletRequest request, HttpServletResponse response) {
          try {
  
              System.out.println("Hello Test");
              // Step 1. Load the JDBC driver
              Class.forName("org.postgresql.Driver");
  
              // Step 2. Create a Connection object
              Connection con = DriverManager.getConnection(
                      "jdbc:postgresql://localhost/test","root", "root!");
  
              Statement s = con.createStatement();
  
              String sql ="SELECT USERS FROM ACCOUNT";
              ResultSet rs = s.executeQuery(sql);
              List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
              while (rs.next()) {
                  list.add(rs.getString("email"));
              }
              String json = new Gson().toJson(list);
              response.getWriter().write(json);
              rs.close();
              s.close();
              con.close();
          } catch (ClassNotFoundException e1) {
              e1.printStackTrace();
          } catch (SQLException e2) {
              e2.printStackTrace();
          } catch (Exception e3) {
              e3.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>And below is my jquery ajax call from the above jsp page as shown in the jsfiddle from where I am calling the above servlet on the click of <code>Process</code> button -</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $('.btn-primary').click(function() {
              alert("Hello");
              $.get('someservlet', function(responseJson) {
                  alert(responseJson);
                  var $ul = $('&lt;ul&gt;').appendTo($('#somediv'));
                  $.each(responseJson, function(index, item) {
                      $('&lt;li&gt;').text(item).appendTo($ul);
                  });
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>But somehow it is not working at all, meaning my servlet is not getting called at all from my JSP page using Ajax. My System.out is also not getting printed on the console as well from the servlet.</p>
  
  <p>Also, I have one more servlet. This is the second servlet I have created. May be that is the reason?</p>
  
  <p><strong>Web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
            http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             version="2.5"&gt;
  
  &lt;/web-app&gt;
  </code></pre>