To create 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 the JSP, use the pageContext implicit object defined by the JSP expression language:
${pageContext.request.requestURL}
Here is a simple example of building a URL on 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 create URLs depending on whether the current URL contains any query string or not . So you add accordingly
?${pageContext.request.queryString}&page=xxx
or simply
?page=xxx
to the current URL.
JSTL and Expression Language were used to implement query string validation. And we used the getRequestURL() method to get the current URL.
source share