简体   繁体   中英

jsp page to servlet through ajax and vice versa

I am sending a orderid from jsp page to servlet through ajax and if the orderid of order table not match it will show an error message in the same jsp page through which request is made fine. Now my question is a when i sending a valid orderid it should forward the page to the other jsp page through servlet which is not happing and also not showing any error

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 code

$('#finduy').click(function(event){

            $.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 code

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;
            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();
        }

You can always send a response code back from the servlet and then do a forward . For example you can send success and in which case you can do document.location to new servlet location.

Also uploading files is now possible in html5 using ajax .

I Got The answer thanks user1889970 your trick helps

i created a new checking point whose work is only to check whether orderid is valid if valid it forward to new page or show invalid

jquery code:

 $("#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 code:

 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;
    }

second part of cmd=single:

    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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM