3

I have a problem reading a value that contains '&' from the url using java spring.

My summary.jsp file contains the following code: `

<h3>
<fmt:message key="contact.title"/>
<c:if test="${!editMode}">
&nbsp;<label class="eightyfivefont">
<a class="termslink eightyfivefont"
          href="?submitForm=true&amp;
            institutionName=<c:out value="${institution.institutionName}" />&amp;
            repositoryName=<c:out value="${institution.repositoryName}" />&amp;
            editMode=true">
 (edit)</a>
</label>
</c:if>
</h3>

`

which produce the following url:

...summary.html?submitForm=true&institutionName=r&r&repositoryName=r&r

The problem is when I am trying to fetch the institutionName that hold the value "r&r".

when fetching the value using the following command:

String name = request.getParameter("institutionName");

it fetches only the string "r" and not "r&r".

The string is stored in XML file as "<institutionName>r&amp;r</institutionName>" which is parsed and added using:

Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("institution");

and for reading from the xml:

Document doc = DocumentHelper.parseText(xml);
Element root = doc.getRootElement();

(I assume the issue isn't with the XML).

Is there a solution for this problem?

Shiva
  • 6,677
  • 4
  • 36
  • 61
Mike
  • 1,007
  • 2
  • 16
  • 33

3 Answers3

1

The ampersand & is used to divide key-value pairs. If you want to have it as a value, or key, for that matter, you have to urlencode it. For example, like this:

encodeURIComponent('&') = "%26"

You should be able to use JavaScript for that. Or, of course, some Java method, if the URL is being created in the jsp itself.

Andreas Gnyp
  • 1,500
  • 1
  • 19
  • 25
0

You should use URLEncoder to encode the institutionName

java.net.URLEncoder.encode("r&r", "UTF-8")

this outputs the URL-encoded, which is fine as a GET parameter:

r%26r
bitkot
  • 4,466
  • 2
  • 28
  • 39
  • How should I use it and combine it with: `institutionName=` ? – Mike Jul 14 '14 at 11:08
  • Simplest way I can think of is to set encoded institution.institutionName from your controller. Better way is to go with what @praki suggested. – bitkot Jul 14 '14 at 11:39
0

check this answer. Create a custom EL function or otherwise you can use scriptlet though it is not recommended.

Community
  • 1
  • 1
whoami
  • 1,517
  • 1
  • 21
  • 29