jsp页面通过ajax到servlet,反之亦然

时间:2013-05-25 20:58:19

标签: java jquery ajax jsp servlets

我通过ajax从jsp页面向servlet发送orderid,如果order表的orderid不匹配,则会显示错误 在同一个jsp页面中的消息,通过该页面使请求正常。现在我的问题是,当我发送有效的orderid时应该 通过servlet将页面转发到另一个jsp页面,这个页面没有任何错误,也没有显示任何错误

jsp page

  <input type="hidden" name=cmd  value="single"/>
    Enter Order No <input type="text" name="oid" id="oidss" /><input type="button" value="Search" id="finduy" />

jquery代码

  

$( '#finduy')。单击(功能(事件){

            $.ajax({
                type:'POST',
                url:"Order",
                style:"full",
                maxRows:12,
                dataType:"json",
                data:{"cmd":"single","oid":encodeURIComponent($('#oidss').val())},

                beforeSend:function(){/*alert("data is sending")*/},
                //problem is here
                success:function(data,textStatus,jqXHR){
                        if(data.success){

                }
                else
                    {
                    alert("invalid Order No");
                    $('#oidss').val("");
                    $('#oidss').focus();


                    }

                },
                error:function(jqXHR, textStatus, errorThrown){
                console.log('textStatus:' + textStatus);
                console.log('errorThrown:' + errorThrown);
                 console.log("Something really bad happened " + textStatus);
                 console.log("jqXHR.responseText " +jqXHR.responseText);

                  $("#ajaxResponse").html(jqXHR.responseText);
            }
            });

servlet代码

  

如果(!(CMD == NULL)&安培;&安培; cmd.equalsIgnoreCase( “单”)){

        OrderDB odb = new OrderDB();
        Order orders =  new Order() ;
        List<OrderDetail> odetaillist = new ArrayList<OrderDetail>();
        List<Order> ordercustlist = new ArrayList<Order>();


        String orderids = request.getParameter("oid");


        try {
            orders = odb.getOrdersById(orderids);
            odetaillist = odb.getOrdersDetailsByOrderId(orderids);
            ordercustlist = odb.getOrderFromCustomer(orderids);
            Customer customerinfo = odb.getOrderCustomer(orderids);
            request.setAttribute("OrderSingle", orders);
            request.setAttribute("OrderDetail", odetaillist);
            request.setAttribute("OrderCust", ordercustlist);
            request.setAttribute("CustomerInfo", customerinfo);
            redirect=orderinfo;
            RequestDispatcher view = request.getRequestDispatcher(redirect);
            view.forward(request, response);

            if(orders.getOrderid() == null){
                JsonObject myobj =  new JsonObject();
                myobj.addProperty("success", false);
                PrintWriter out = response.getWriter();
                out.print(myobj);
                out.close();
                return;
            }



        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

2 个答案:

答案 0 :(得分:0)

您始终可以从servlet发回响应代码,然后执行转发。例如,您可以发送成功,在这种情况下,您可以将document.location执行到新的servlet位置。

现在可以使用ajax在html5中上传文件。

答案 1 :(得分:0)

我得到了答案感谢user1889970你的诀窍帮助

我创建了一个新的检查点,其工作只是检查orderid是否有效,如果有效转发到新页面或显示无效

jquery代码:

 $("#find").click(function(event){
    var cname = $('#oidss').val();
        $.ajax({
            type:'POST',
            url:"Order",
            style:"full",
            maxRows:12,
            dataType:"json",
            data:{"cmd":"find","oid":encodeURIComponent(cname)},

            beforeSend:function(){/*alert("data is sending")*/},
            success:function(data,textStatus,jqXHR){
                    if(data.success){
                        window.location.href='Order?cmd=single&oid='+cname;  
                }
            else
                {
                alert("Invalid Order No.");
                 $('#oidss').val("");
                 $('#oidss').focus();
                }

            },
            error:function(jqXHR, textStatus, errorThrown){
            console.log('textStatus:' + textStatus);
            console.log('errorThrown:' + errorThrown);
             console.log("Something really bad happened " + textStatus);
             console.log("jqXHR.responseText " +jqXHR.responseText);

        }
        });
    });

servlet代码:

 else if(!(cmd==null)&&cmd.equalsIgnoreCase("find")){

        OrderDB odb         =   new OrderDB();
        Order orders        =   new Order() ;
        JsonObject myobj    =   new JsonObject();

        PrintWriter out = response.getWriter();
        String orderids = request.getParameter("oid");

        try {orders = odb.getOrdersById(orderids);
        if(orders.getOrderid() != null){
            myobj.addProperty("success", true);
        }
        else{
            myobj.addProperty("success", false);
        }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       out.print(myobj);
        out.close();
        return;
    }

cmd的第二部分=单身:

    if(!(cmd==null)&&cmd.equalsIgnoreCase("single")){

        OrderDB odb = new OrderDB();
        Order orders =  new Order() ;
        List<OrderDetail> odetaillist = new ArrayList<OrderDetail>();
        List<Order> ordercustlist = new ArrayList<Order>();
        String orderids = request.getParameter("oid");


        try {
            orders = odb.getOrdersById(orderids);
            odetaillist = odb.getOrdersDetailsByOrderId(orderids);
            ordercustlist = odb.getOrderFromCustomer(orderids);
            Customer customerinfo = odb.getOrderCustomer(orderids);
            request.setAttribute("OrderSingle", orders);
            request.setAttribute("OrderDetail", odetaillist);
            request.setAttribute("OrderCust", ordercustlist);
            request.setAttribute("CustomerInfo", customerinfo);
            redirect=orderinfo;

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }