john doe
john doe

Reputation: 133

request.getParameter("pgIndex") always returns null in the servlet

I am trying to paginate rows of a table inside my servlet using hibernate.But once I click on the desire index of the page it always gives me only the first set of row of the table.So I put System.out.print() at every major sections and finally found out that the request.getParameter("pgIndex") is always returns null.

My servlet code:

    int pageIndex = 0;
    int totalNumberOfRecords = 0;
    int numberOfRecordsPerPage = 5;

    String sPageIndex = request.getParameter("pgIndex");
 //whether pgIndex=1 or pgIndex=2 in the url, always returns null as the output.
    System.out.println("pg - " + sPageIndex);
    pageIndex = sPageIndex == null ? 1 : Integer.parseInt(sPageIndex);

    int s = (pageIndex * numberOfRecordsPerPage) - numberOfRecordsPerPage;


List<ProductHasSize> phs = ses.createCriteria(ProductHasSize.class)
                .setFirstResult(s)
                .setMaxResults(numberOfRecordsPerPage)
                .list();

        for (ProductHasSize pro : phs) {... some html content here...}


      Criteria criteriaCount = ses.createCriteria(ProductHasSize.class);
        criteriaCount.setProjection(Projections.rowCount());
        totalNumberOfRecords = (int) (long) (Long) criteriaCount.uniqueResult();

        int noOfPages = totalNumberOfRecords / numberOfRecordsPerPage;

        if (totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage)) {
            noOfPages = noOfPages + 1;
        }

        for (int j = 1; j <= noOfPages; j++) {
            String myurl = "products.jsp?pgIndex=" + j;
            String active = j == pageIndex ? "active" : "";
            s2 = s2 + "<li class='" + active + "'><a href=" + myurl + ">" + j + "</a></li>";

        }

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write("[{\"d1\":\"" + s1 + "\",\"d2\":\"" + s2 + "\"}]");

products.jsp

<div class="row">
      <div class="col-md-12">
           <ul class="pagination" id="pagId"></ul>
       </div>
</div>

JavaScript

$(document).ready(function () {
                $.ajax({
                    url: 'AdimnProductFilterAction',
                    dataType: 'json',
                    cache: false,
                    success: function (data) {
                        $.each(data, function (key, value) {
                            $('#proFilterTab').html(value.d1);
                            $('#pagId').html(value.d2);
                        });
                    },
                    error: function () {
                        alert('error');
                    }
                });

            });

UPDATE :

 $(document).on("click", "#pagId a", function (event) {
    //tried with adding another function . But still returns null.
    event.preventDefault();
    var para = $(this).attr('href').match(/\d+/);
    $.ajax({
        url: 'AdimnProductFilterAction',
        dataType: 'json',
        data: {pgIndex: para},
        cache: false,
        success: function (data) {
            $.each(data, function (key, value) {
                $('#proFilterTab').html(value.d1);
                $('#pagId').html(value.d2);
            });
        },
        error: function () {
            alert('error');
        }
    });
});

Thanks in advance.

Upvotes: 0

Views: 425

Answers (1)

Jan
Jan

Reputation: 13858

In sending JSON data, you will not simply receive it as request parameter. Instead, just add "normal" parameter:

Sending as HTTP POST

$.ajax({
    url: 'AdimnProductFilterAction',
    type: 'POST',
    data: {
       'pgIndex': para
    },
    cache: false,
    success: function (data) {
        $.each(data, function (key, value) {
            $('#proFilterTab').html(value.d1);
            $('#pagId').html(value.d2);
        });
    },
    error: function () {
        alert('error');
    }
});

Or as HTTP GET

$.ajax({
    url: 'AdimnProductFilterAction?pgIndex='+para,
    cache: false,
    success: function (data) {
        $.each(data, function (key, value) {
            $('#proFilterTab').html(value.d1);
            $('#pagId').html(value.d2);
        });
    },
    error: function () {
        alert('error');
    }
});

To add parameter into your servlet call.

Upvotes: 2

Related Questions