8

Lets say my current URL is: /app.jsp?filter=10&sort=name.

I have a pagination component in JSP which should contain links like:
/app.jsp?filter=10&sort=name&page=xxx.

How do I create valid URLs in JSP by adding new parameters to current URL? I dont want use Java code in JSP, nor end up with URLs like:
/app.jsp?filter=10&sort=name&?&page=xxx, or /app.jsp?&page=xxx, etc.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
igo
  • 6,359
  • 6
  • 42
  • 51

3 Answers3

12

Ok, I found answer. First problem is that I have to preserve all current parameters in URL and change only page parameter. To do this I have to iterate over all current parameters and add those I don't want to change to URL. Then I added parameters I want to either change or add. So I ended up with solution like this:

<c:url var="nextUrl" value="">
    <c:forEach items="${param}" var="entry">
        <c:if test="${entry.key != 'page'}">
            <c:param name="${entry.key}" value="${entry.value}" />
        </c:if>
    </c:forEach>
    <c:param name="page" value="${some calculation}" />
</c:url>

This will create nice and clean URL independent of page parameter in request. Bonus to this approach is that URL can be just anything.

Josh Johnson
  • 10,729
  • 12
  • 60
  • 83
igo
  • 6,359
  • 6
  • 42
  • 51
9
<c:url var="myURL" value="/app.jsp">
   <c:param name="filter" value="10"/>
   <c:param name="sort" value="name"/>
</c:url>

To show the url you can do something like this

<a href="${myURL}">Your URL Text</a>
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • unfortunately my pagination component (because of reuse) don't know which parameter are valid for URL and also cannot set them by it's self. It can only add parameter page – igo Mar 29 '13 at 18:54
  • then you can check it your code is here –  Mar 29 '13 at 18:59
  • i.e adding parameter page `` where `page` is a request attr and will be parameter in the url, if you need parameter instead of attr then use `param` prefix. –  Mar 29 '13 at 19:04
2

To construct a new URL based on the current URL, you first need to get the current URL from the request object. To access the request object in a JSP use pageContext implicit object defined by the JSP expression language:

${pageContext.request.requestURL}  

Here is the simple example of constructing URL in a JSP page:

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <h1>Testing URL construction</h1>
    <c:choose>
        <c:when test="${pageContext.request.queryString != null}">
            <a href="${pageContext.request.requestURL}?${pageContext.request.queryString}&page=xxx">Go to page xxx</a>
        </c:when>
        <c:otherwise>
            <a href="${pageContext.request.requestURL}?page=xxx">Go to page xxx</a>
        </c:otherwise>
    </c:choose>
</body>
</html>


This solution allows you to construct URLs depending on whether the current URL already contains some query string or not. So you respectively append either

?${pageContext.request.queryString}&page=xxx

or just

?page=xxx

to the current URL.

JSTL and the Expression Language were used to implement checking for a query string. And we used getRequestURL() method to obtain the current URL.

informatik01
  • 16,038
  • 10
  • 74
  • 104