Javascript getParameter(“pgIndex”)在servlet中始终返回null

Javascript getParameter(“pgIndex”)在servlet中始终返回null,javascript,java,hibernate,jsp,servlets,Javascript,Java,Hibernate,Jsp,Servlets,我试图使用hibernate对servlet中的一个表的行进行分页。但是,一旦我单击页面的desire索引,它总是只给我表的第一行。因此,我在每个主要部分放置System.out.print(),最后发现request.getParameter(“pgIndex”)始终返回null 我的servlet代码: int pageIndex = 0; int totalNumberOfRecords = 0; int numberOfRecordsPerPage = 5;

我试图使用hibernate对servlet中的一个表的行进行分页。但是,一旦我单击页面的desire索引,它总是只给我表的第一行。因此,我在每个主要部分放置
System.out.print()
,最后发现
request.getParameter(“pgIndex”)
始终返回null

我的servlet代码:

    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 + "\"}]");
 $(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');
        }
    });
});
更新:

    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 + "\"}]");
 $(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');
        }
    });
});
提前感谢。

在发送JSON数据时,您不会简单地将其作为请求参数接收。相反,只需添加“正常”参数:

以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');
    }
});
或者作为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');
    }
});
将参数添加到servlet调用中。

在发送JSON数据时,您不会简单地将其作为请求参数接收。相反,只需添加“正常”参数:

以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');
    }
});
或者作为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');
    }
});

将参数添加到servlet调用中。

products.jsp doen的handle pgIndex和pgIndex从未传递到servlet中(如果servlet链接到ajax调用?!)@Jan如果servlet链接到ajax调用,这是什么意思<代码>url:'AdimnProductFilterAction',。AdimnProductFilterAction是我的servlet名称。所以我把它联系起来了。这就是我猜的。您永远不会将参数传递到该servlet中。没有?pgIndex=也没有数据发送方式post@Jan那么关于
String myurl=“products.jsp?pgIndex=“+j;字符串活动=j==pageIndex?“主动”:“主动”;s2=s2+“??它不是传递到servlet吗???但这是在servlet生成代码来调用product.jsp的内部-其中该参数似乎未使用products.jsp的句柄pgIndex,并且pgIndex从未传递到您的servlet(如果servlet链接到您的ajax调用?!)@Jan如果servlet链接到您的ajax调用,这是什么意思<代码>url:'AdimnProductFilterAction',
。AdimnProductFilterAction是我的servlet名称。所以我把它联系起来了。这就是我猜的。您永远不会将参数传递到该servlet中。没有?pgIndex=也没有数据发送方式post@Jan那么关于
String myurl=“products.jsp?pgIndex=“+j;字符串活动=j==pageIndex?“主动”:“主动”;s2=s2+“??它不是传递到servlet中吗???但这是在servlet中生成代码来调用product.jsp的地方-该参数似乎没有使用-非常感谢您的时间。我尝试了近3天来解决这个问题。你救了我。:)非常感谢您抽出时间。我花了将近3天的时间来解决这个问题。你救了我。:)