2

I have a servlet running on tomcat 6 which should be called as follows:

http://<server>/Address/Details?summary="Acme & co"

However: when I iterate through the parameters in the servlet code:

//...
while (paramNames.hasMoreElements()) {
    paramName = (String) paramNames.nextElement();
    if (paramName.equals("summary")) {
        summary = request.getParameter(paramName).toString();
    }
}
//...

the value of summary is "Acme ".

I assume tomcat ignores the quotes - so it sees "& co" as a second parameter (albeit improperly formed: there's no =...).

So: is there any way to avoid this? I want the value of summary to be "Acme & co". I tried replacing '&' in the URL with &amp; but that doesn't work (presumably because it's decoded back to a straight '&' before the params are parsed out).

Thanks.

sfinnie
  • 9,854
  • 1
  • 38
  • 44

3 Answers3

1

Are you encoding and decoding the URL with URLEncode ? If so, can you check what the input and output of those are ? Seems like one of the special characters is not being properly encoded/decoded

Try %26 for the &

avrono
  • 1,648
  • 3
  • 19
  • 40
  • +1 for the '%26' suggestion. Apparently you shouldn't use URLEncoder to encode URLs: http://stackoverflow.com/questions/4571346/how-to-encode-url-to-avoid-special-characters-in-java. – sfinnie Sep 27 '13 at 20:58
1

Use http://<server>/Address/Details?summary="Acme %26 co". Because in URL special http symbol(e.g. &,/, //) does not work as parameters.

Masudul
  • 21,823
  • 5
  • 43
  • 58
1

Try your parameter like

summary="Acme &amp; co"

& is part reserved characters. Refer RFC2396 section 2.2. Reserved Characters.

how to encode URL to avoid special characters in java

Characters allowed in GET parameter

HTTP URL - allowed characters in parameter names

http://illegalargumentexception.blogspot.in/2009/12/java-safe-character-handling-and-url.html

Community
  • 1
  • 1
Alpesh Gediya
  • 3,706
  • 1
  • 25
  • 38