Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to URL encode a URL in JSP / JSTL?

Tags:

java

html

jsp

I want to create an encoded URL for my site. Say for example, for this URL: "http://google.com/index.html"

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

like image 835
Abrah Avatar asked Apr 10 '13 10:04

Abrah


2 Answers

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="http://google.com/index.html" />
</c:url>

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

This will result in:

<a href="/yourClient?yourParamName=http%3a%2f%2fgoogle.com%2findex.html">Link to your client</a>
like image 125
Jasper de Vries Avatar answered Oct 05 '22 07:10

Jasper de Vries


Using UrlEncoder.encode() is the answer. But the point is that this method doesn't percentage encode. Use:

java.net.UrlEncoder.encode(stringOfURL,"UTF-8").replace("+","%20")
like image 27
Mohsen Abasi Avatar answered Oct 05 '22 09:10

Mohsen Abasi