Search code examples
javaajaxjspservletsxmlhttprequest

Calling jsp inside a servlet with another new parameter


I have a jsp (items.jsp) which iterate product list and display result products.In its scrip-let I iterate the products list like below:

<jsp:useBean id="items" scope="page" class="com.certus.controllers.ItemPage"/>

<%
  List<Product> products = null;

   //check if the parameter "sort" available or not
    if (request.getParameter("sort") != null) {
        System.out.print("Three parameter method");
        products = items.filterProducts(Integer.parseInt(request.getParameter("sub")),
                Integer.parseInt(request.getParameter("cat")),request.getParameter("sort"));
    } else {
        System.out.print("Two parameter method");
        products = items.filterProducts(Integer.parseInt(request.getParameter("sub")),
                Integer.parseInt(request.getParameter("cat")));
    }
    for (Product p : products) {
%>

  some html content here....

Up to this stage everything works fine.In this same jsp I have a selectbox which I have introduced to sort the products as the client needs. Once I select an item from the selectbox it sends a request to the servlet and nothing wrong with that ajax request too. (Test with System.out.println() and got the required results from the glass-fish sever.)

From there on wards I came up with a problem. Here is my servlet :

     String category = request.getParameter("category");
        String sub = request.getParameter("sub_category");
        String sort = request.getParameter("sort");

//Tried with both methods but no luck
   //     response.sendRedirect("items.jsp?cat=" + category + "&sub="+ 
  //sub + "&sort=" + sort);

RequestDispatcher RequetsDispatcherObj = request.getRequestDispatcher("items.jsp?cat=" + category + "&sub=" + sub + "&sort=" + sort);
RequetsDispatcherObj.forward(request, response);

As you can see I'm trying to redirect to the items.jsp with another parameter included with the url (sort) to call the three para-metered method which was inside the items.jsp.

All the suggestions are welcome to figure out where I have made the mistake. In case you need my ajax call here it is:

<script type="text/javascript">
    $(document).on("change", "#sortCombo", function () {
        $.ajax({
            url: 'sortProducts',
            data: {category: $('#category').val(), sub_category: $('#sub_category').val(), sort: $('#sortCombo').val()},
            type: 'get',
            cache: false,
            success: function (data) {
               // alert(data);
                // $('#somediv').text(responseText);
            },
            error: function () {
                alert('error');
            }
        }
        );
    });
</script>

sortProducts is the servlet name.And also need to tell you write on the url with sort parameter and refresh the page works fine and giving the required result which display products sorted by name,price ratings etc.. Thank you.

UPDATE

If I can call the method(Three parameter one) by writing on the URL why not the ajax response do the same with response.sendRedirect("items.jsp?cat=" + category + "&sub=" + sub + "&sort=" + sort); ???


Solution

  • Finally ,I came up with a solution. May be not the most appropriate one. I changed my servlet code to:

            response.getWriter().write(category+"-"+sub+"-"+sort);
    //        RequestDispatcher RequetsDispatcherObj = request.getRequestDispatcher("items.jsp");
    //        RequetsDispatcherObj.forward(request, response);
    

    And my ajax success to :

    success: function (data) {
       var split = data.split("-");
       document.location.href = "http://localhost:8080/ECommerceApp/items.jsp?cat=" + split[0] + "&sub=" + split[1] + "&sort=" + split[2];
                    },
    

    Thank you every one who helped me even by a comment.They mean lot to me. :)