Reputation: 39
I have a parameter in JSP URL as
'http://www.mydoamin.com':8080/?mylink=http://www.myweb.com/#12345'
I want to get the value of parameter mylink
in my jsp as:
<%
String conferencelink = request.getParameter("mylink").toString();
out.println(conferencelink);
%>
the #after
value is not getting printed.
Can anyone guide me how I can solve this? Please note the source URL parameter can not be changed from # format.
Upvotes: 0
Views: 730
Reputation: 1108632
http://www.mydomain.com:8080/?mylink=http://www.myweb.com/#12345
This URL is in invalid format. Your mistake is that you didn't URL-encode the query string parameter value of mylink
, in contrary to the HTTP specification. This way, the #
part is incorrectly been interpreted as an URL fragment identifier of the main URL on http://www.mydomain.com:8080
.
This URL is in valid format:
http://www.mydomain.com:8080/?mylink=http%3A%2F%2Fwww.myweb.com%2F%2312345
You can create such an URL as follows in Java:
String url = "http://www.mydomain.com:8080/?mylink=" + URLEncoder.encode("http://www.myweb.com/#12345", "UTF-8");
Or as follows in JSP:
<c:url var="url" value="http://www.mydomain.com:8080/">
<c:param name="mylink" value="http://www.myweb.com/#12345" />
</c:url>
<a href="${url}">link</a>
Either way, the request parameter will now be properly decoded by the servletcontainer:
String mylink = request.getParameter("mylink");
Please note that the decoding step takes place fully transparently, you don't need to use URLDecoder
or so. Please also note that the toString()
call afterwards is removed as it makes no utter sense. It returns String
already and calling toString()
without null-checking would only end up in NullPointerException
when the parameter is not been specified at all.
Upvotes: 1