Ajax没有将数据转发到servlet

时间:2013-05-23 19:10:54

标签: jquery ajax servlets

HTML文件的下拉列表会动态填充。 在这里,我通过Ajax将表单值传递给servlet,servlet生成表格数据,该表格数据被附加到表格中。我面临的问题是通过Ajax发送参数。

Servlet(DealerSearch.java) -

public class DealerSearch extends connection {
    private static final long serialVersionUID = 1L;
    String sql,dname,i,table;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
        get_connection();
            if(request.getParameter("dsel")!=null)
            {
                dname = request.getParameter("dsel");
            }
            if(request.getParameter("name")!=null)
            {
                 dname = request.getParameter("dname");
            }
            System.out.println(dname);
            sql = "select * from dealers_info where dealer_name='"+dname+"'";
            st = con.prepareStatement(sql);
            rs = st.executeQuery();

            table="<thead>" +
                    "<tr>" +
                    "<td>Dealer ID</td>" +
                    "<td>Dealer Name</td>" +
                    "<td>Dealer Address</td>" +
                    "<td>Region</td>" +
                    "<td>Area</td>" +
                    "<td>Contact Person</td>" +
                    "<td>Contact Number(O)</td>" +
                    "<td>Contact Number(M)</td>" +
                    "</tr></thead><tbody>";

            while(rs.next())
            {   table+="<tr>";
                table+="<td>"+rs.getString("Dealer_ID")+"</td>";
                table+="<td>"+rs.getString("Dealer_name")+"</td>";
                table+="<td>"+rs.getString("Dealer_address")+"</td>";
                table+="<td>"+rs.getString("Region_Name")+"</td>";
                table+="<td>"+rs.getString("Area_Name")+"</td>";
                table+="<td>"+rs.getString("Contact_Person")+"</td>";
                table+="<td>"+rs.getString("Contact_No_Office")+"</td>";
                table+="<td>"+rs.getString("Contact_No_Mobile")+"</td>";
                table+="</tr>";
            }
            table+="</tbody><table>";
            System.out.println(table);
            response.getWriter().print(table);
            st.close();
            rs.close();
            close_connection();




        }
        catch(Exception e)
        {
        e.printStackTrace();
        }

    }

}

HTML -

<form method="post" id="FindDealer" name="FindDealer" class="form-horizontal">

  <br/> <br/>
      <h2 style="text-align:center;">Dealers</h2>

      <p style="text-align:center;font-family:helvetica;font-size:20px;">Select a dealer, or type in a dealer name, for details.</p>
      <br/>
      <div class="control-group" id="one">
        <label class="control-label" for="input01">Dealer Name</label>
        <div class="controls">
          <input type="text" class="input-xlarge" name="dname" id="input01" onClick="return checkDisabling();" data-provide="typeahead" autocomplete="off" data-source="dealernameods">
        </div>
      </div>
      <p style="text-align:center;font-family:helvetica;font-size:20px;">OR</p>
      <div class="control-group">
        <label class="control-label" for="select01">Dealer Name</label>
        <div class="controls">
          <select id="dealerselect" name="dsel">

          </select>
        </div>
      </div>

     <div style="text-align:center;font-family:helvetica;font-size:30px;">
        <button type="submit" class="btn btn-primary" onSubmit="return onsearch();"><i class="icon-search"></i> Find Dealer</button>
        <button type="reset" class="btn"><i class="icon-trash"></i> Cancel</button>
      </div>

  </form>
   <table class="table table-bordered table-striped table-hover" id="dealertable">
 </table>

功能 -

  function onsearch()
    {
        /* stop form from submitting normally */
        e.preventDefault();
    alert($('#dealerselect').val());
          $.ajax({
                type: "post",
                url: "/SalesTrackingTool/DealerSearch",
                data:{ dsel: $('#dealerselect').val()},
                cache:false
          }).done(function(data) {
                  $('#dealertable').empty().append(data);
      });

    }

1 个答案:

答案 0 :(得分:0)

在ajax请求参数中尝试将url参数替换为完整名称,如:

url: "http:localhost:port/SalesTrackingTool/DealerSearch",

这可能会触及servlert