How to remove previous request parameter from URL query string

40 views Asked by At

when i click on one category, it show the list of products of this category, the url will be:

http://localhost:9999/toyshop/home?category=Đồ%20chơi%20theo%20phim

but when I click on the next pagination page, the url will be

http://localhost:9999/toyshop/home?category{Đồ%20chơi%20theo%20phim}=Đồ%20chơi%20theo%20phim&page=2

This is my jsp code:

<c:remove var="param" scope="session" />
    <c:remove var="paramValue" scope="session" />
    <c:remove var="href" scope="session" />

    <c:remove var="param" scope="request" />
    <c:remove var="paramValue" scope="request" />
    <c:remove var="href" scope="request" />
    <c:choose>
        <c:when test="${not empty sessionScope.productName}">
            <c:set var="servlet" value="search"/>
            <c:set var="param" value="product"/>
            <c:set var="paramValue" value="${sessionScope.productName}"/>
        </c:when>
        <c:when test="${not empty sessionScope.categoryName}">
            <c:set var="servlet" value="home"/>
            <c:set var="param" value="category"/>
            <c:set var="paramValue" value="${sessionScope.categoryName}"/>
        </c:when>
        <c:otherwise>
            <!-- Handle the case where both are empty -->
            <c:set var="servlet" value="home"/>
            <c:set var="param" value=""/>
            <c:set var="paramValue" value=""/>
        </c:otherwise>
    </c:choose>

    <c:choose>
        <c:when test="${empty paramValue}">
            <c:set var="href" value="${servlet}?"/>
        </c:when>
        <c:otherwise>
            <c:set var="href" value="${servlet}?${param}=${paramValue}"/>
        </c:otherwise>
    </c:choose>


    <c:if test="${currentPaginationPage > 1}">
        <a href="${href}&page=${currentPaginationPage - 1}" class="pagination__item">
            <i class="pagination__icon fas fa-chevron-left"></i>
        </a>
    </c:if>

    <c:if test="${startPage > 1}">
        <div class="pagination__item pagination__item--none">...</div>
    </c:if>

    <c:forEach begin="${startPage}" end="${endPage}" var="page">
        <c:choose>
            <c:when test="${page == currentPaginationPage}">
                <a href="${href}&page=${page}" class="pagination__item pagination__item--active">${page}</a>
            </c:when>
            <c:otherwise>
                <a href="${href}&page=${page}" class="pagination__item">${page}</a>
            </c:otherwise>
        </c:choose>
    </c:forEach>

    <c:if test="${endPage < totalPaginationPage}">
        <div class="pagination__item pagination__item--none">...</div>
    </c:if>

    <c:if test="${currentPaginationPage < totalPaginationPage}">
        <a href="${href}&page=${currentPaginationPage + 1}" class="pagination__item">
            <i class="pagination__icon fas fa-chevron-right"></i>
        </a>
    </c:if>

I'm expecting the url will be

http://localhost:9999/toyshop/home?category=Đồ%20chơi%20theo%20phim&page=2
1

There are 1 answers

0
BalusC On

The ${param} is a reserved variable in EL. It's basically an implicit object facade of HttpServletRequest#getParameter(). You should not try to remove/overwrite it.

Use a different name than ${param}. E.g. ${paramName}.

<c:set var="paramName" value="category"/>
<c:set var="paramValue" value="${sessionScope.categoryName}"/>
<c:set var="href" value="${servlet}?${paramName}=${paramValue}"/>

See also: