How to URL encode a URL in JSP?

Asked by At

I want to create an encoded URL for my site. Say for example, for this URL: ""

I want to give this URL to the client by URL encoding it.

4 Answers

Jasper de Vries On Best Solutions

Since you are using JSP, I would stick to JSTL and not use scriptlets. You could use the JSTL tag <c:url /> in combination with <c:param />:

<c:url value="/yourClient" var="url">
  <c:param name="yourParamName" value="" />

<a href="${url}">Link to your client</a>

This will result in:

<a href="/yourClient?">Link to your client</a>
Deepak On

Try in your JSP code:

Base64.encodeBase64( "")
Mohsen Abasi On

Using UrlEncoder.encode() is the answer. But the point is that this method doesn't percentage encode. Use:,"UTF-8").replace("+","%20")
Sebastián Ezquerro On

The accepted answer is missing some JSP code to be valid, it should be:

<c:url value="/yourClient" var="url">
  <c:param name="yourParamName" value="" />

<a href="<c:out value='${url}'/>">Link to your client</a>

As a comment pointed out, another option is to use JavaScripts encodeURIComponent method.