4

When I use the below jstl code

  <a href="http://mysite.com?id="<c:out value="${myid}"/>/><c:out value="${myid}"/></a> 

the output is :

"1234"

The value 1234 corresponds to the variable value of myid but the url being generated is "http://mysite.com?id=" so no value for myid is being generated as part of the href.

How can I amend the href so that entire href is displayed :

"http://mysite.com?id=1234"

instead of :

"http://mysite.com?id="

blue-sky
  • 51,962
  • 152
  • 427
  • 752

2 Answers2

22

Ultimately, JSP/JSTL generates HTML. You're familiar with basic HTML, right?

Look closer at the generated HTML output by rightclick, View Source in browser. You'll see:

<a href="http://mysite.com?id="1234/>1234</a> 

Is that valid HTML? No, you're closing the attribute value too soon with " at wrong place and you're closing the tag too soon with />. Look, the Stack Overflow HTML syntax highlighter also got confused. Instead, it should have been:

<a href="http://mysite.com?id=1234">1234</a> 

Fix the HTML generator (i.e. the JSP/JSTL code) accordingly so that it generates the desired HTML:

<a href="http://mysite.com?id=<c:out value="${myid}"/>"><c:out value="${myid}"/></a> 

Unrelated to the concrete problem, the <c:out> is only helpful in preventing XSS attack holes when redisplaying user-controlled input and actually the wrong tool to inline URL parameters. If you can guarantee that ${myid} is always a number (because it's a Long or Integer), you can even just leave it entirely out, making the code prettier to read:

<a href="http://mysite.com?id=${myid}">${myid}</a> 

If the ${myid} is however not a guaranteed to be a number (because it's a String), then you should use <c:url> and <c:param> to properly URL-encode it:

<c:url value="http://mysite.com" var="myURL">
    <c:param name="id" value="${myid}" />
</c:url>
<a href="${myURL}"><c:out value="${myid}" /></a>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 for going beyond original question to help create a better example with explanation why you went in that direction. – TechTrip Aug 19 '14 at 14:04
3

<c:url> tag is used to create an url. It is helpful in the case when cookies is turned off by the client, and you would be required to rewrite URLs that will be returned from a jsp page.

<c:param> tag may used as a subtag of to add the parameters in the returned URL. Using these parameters encodes the URL.

<c:url value="http://mysite.com" var="myURL">
   <c:param name="id" value="${myid}" />
</c:url>

<a href="${myURL}" />${myURL}</a>

Read more from here.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
  • 1
    1) You're nowhere *explaining* the problem and the solution, causing the OP or any other future reader to not *understand* the problem and the solution at all. Therefore, I would consider your answer as unhelpful or at least as low quality and thus absolutely not a single upvote worthy. 2) The code snippet in your answer still generates invalid HTML. – BalusC Aug 06 '13 at 16:13
  • @BalusC: OP asked "How to mix href within jstl code". And I provided him a better and cleaner way to do that. – Ankur Lathi Aug 06 '13 at 16:21
  • You really can't be bothered about the quality of Stack Overflow, right? This is just so sad. – BalusC Aug 06 '13 at 16:36
  • 1
    @BalusC: Whats the problem with my answer?? – Ankur Lathi Aug 06 '13 at 16:54