The part after # is missing from the request parameter value

I made a hello world web application in Java on a Tomcat container. I have a query string

code=askdfjlskdfslsjdflksfjl#_=_ 

with underscores on either side of = in the URL. When I tried to get the query string in the request.getParameter("code") servlet, I only get askdfjlskdfslsjdflksfjl . The part after # missing.

How is this caused and how can I solve it?

+4
source share
3 answers

This is because the part of the URL after # is not part of the request.

Section 3.4 of the relevant RFC says:

The request component is indicated by the first question mark ("?") And ends with a number ("#") or at the end of the URI.

+5
source

# interpreted only by the browser, not the server. If you want to pass the # character to the server, you must URLEncode.

Example:

 URLEncoder.encode("code=askdfjlskdfslsjdflksfjl#=", "UTF-8"); 
+1
source

Read percent coding on Wikipedia . # and = reserved characters in URLs. Only non-reserved characters can be used equal by URL, all other characters must be URL-encoded . URL encoded value # is %23 and = is %3D . So what this should do:

 code=askdfjlskdfslsjdflksfjl%23_%3D_ 

If this really comes from an HTML <a> link in some JSP, for example:

 <a href="servletUrl?code=askdfjlskdfslsjdflksfjl#_=_">some link</a> 

then you really had to change it to use JSTL <c:url>

 <c:url var="servletUrlWithParam" value="servletUrl"> <c:param name="code" value="askdfjlskdfslsjdflksfjl#_=_" /> </c:url> <a href="${servletUrlWithParam}">some link</a> 

so that it is generated as

 <a href="servletUrl?code=askdfjlskdfslsjdflksfjl%23_%3D_">some link</a> 

Please note that this does not apply to Java / Servlets per se, this applies to all web applications.

+1
source

Source: https://habr.com/ru/post/1386359/


All Articles