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.
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"%>
Test Page
Testing URL construction
Go to page xxx
Go to page xxx
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.