无法使用jquery-AJAX从JSP页面向Servlet发送FormData

时间:2014-03-30 05:21:40

标签: java jquery ajax jsp servlets

尝试使用jQuery-AJAX将JSP数据从JSP文件发送到servlet,但在servlet post请求中获取null值。

在JSP页面上,在控制台中它显示正确的数据,在post请求中它显示它正在发送数据,我已经在mozilla中使用firebug检查了它。但是在servlet中获取空值。

JSP的代码:

        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

        <script src="js/jquery-1.8.3.js" type="text/javascript"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.24.js"></script>
        <script type="text/javascript">
            $( document ).ready(function() {

    alert("DOM is ready");

});

             function qry()
            {

                var fd = new FormData();                               
                var tinfo=$("#tablename").val();                   
                var cinfo=$("#columnname").val();
                var oinfo=$("#operator").val();
                var uinfo=$("#txtuserinput").val();
                 console.log(tinfo);
                console.log(cinfo);
                console.log(oinfo);
                console.log(uinfo);

                fd.append("abc",tinfo);
            fd.append("mycolumnname",cinfo);
            fd.append("myoperator",oinfo);
            fd.append("mytxtuserinput",uinfo);

                 $.ajax({       

                                type: 'POST',
                                url: "http://localhost:8084/NavigatingApp/getInfo/queryoutput",     
                                contentType: false,
                                dataType: "html",
                                data: fd,                                
        async:false,            
        cache: false,

        processData: false,                

                success:function(data){
                    console.log(data);

                        },
        error:function(){
            alert("Error in receving data...!!!");
        }
    });
            }
            </script>
    </head>
    <body>
        <h1>Hello World!</h1>
        <select name="tablename" id="tablename" >
            <option value="tbl1">tbl1</option>
            <option value="tbl2">tbl2</option>
            <option value="tbl3">tbl3</option>            
        </select>
        <select name="columnname" id="columnname">
            <option value="col1">col1</option>
            <option value="col2">col2</option>
            <option value="col3">col3</option>
        </select>
        <select name="operator" id="operator">
            <option value="op1">op1</option>
            <option value="op2">op2</option>
            <option value="op3">op3</option>
        </select>
        <select name="condition" id="condition" >
            <option value="condtn1">condtn1</option>
            <option value="condtn2">condtn2</option>
            <option value="condtn3">condtn3</option>
        </select>
        <input type="text" name="txtuserinput" id="txtuserinput" />
          <input type="button" value="search" id="btnsearch" onclick="qry();" />
    </body>
</html>

Servlet代码:

@WebServlet(name = "queryoutput", urlPatterns = {"/queryoutput"})
public class QueryOutput extends HttpServlet {
 public QueryOutput() {
        super();
        // TODO Auto-generated constructor stubatavu

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


            response.setContentType("application/json;charset=UTF-8");


            System.out.println(" inside do post table::" +request.getParameter("mytablename"));
            System.out.println(" inside do post col name:: " +request.getParameter("mycolumnname"));
            System.out.println(" inside do post operator::" +request.getParameter("myoperator"));
            System.out.println(" inside do post user input::" +request.getParameter("mytxtuserinput"));
              response.setContentType("application/json;charset=UTF-8");

            PrintWriter out=response.getWriter();


              out.flush();

    }    
}

1 个答案:

答案 0 :(得分:0)

您根本不发送数据

data: fd,

尝试;

data: {fd: fd}