• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about <c:param> in JSTL

 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone,

I'm back, almost 6 weeks after passing SCJP 1.6. The job search has been a miserable experience. But I am enjoying my studies for SCWCD, which I hope to take in a few months.

And now, my first SCWCD question:

From what I read, when we use <c:param> in the body of a <c:url>, this performs URL encoding (e.g. changing a space to a plus sign). Apparently, if we don't use <c:param>, <c:url> will do URL rewriting (adding session info to the URL), but will NOT do URL encoding.

Assuming the above is true, I'm curious... why would anyone want to do URL rewriting, and at the same time, NOT want to do URL encoding? Why would anyone use <c:url> without one or more <c:param>'s?

Also, is there some reason the syntax was decided upon this way; I would think it would be more natural to have an attribute, like encodeURL="true", for <c:url>. It seems strange to me that the default, for <c:url> is: URL rewriting yes, URL encoding no.

Thanks!!!
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<c:url> takes care of session tracking, which means appending the session id to the url in case it is needed.

<c:param> is for parameters. It is a more elegant approach to have each param as one tag instead of coding



using



looks much cleaner. Also, the way the parameters are concatenated is now up to the container, maybe in 55 years somebody decides it's much cooler to use ; instead of & to concatenate parameters ;)
 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian, thank you very much for your response to the second question.

I'm still really curious about my first question:

Why would anyone want to do URL rewriting, and at the same time, NOT want to do URL encoding?
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be a separation of concerns.

the <c:out tag encodes for you, <c:url rewrites

 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Sebastian; that makes sense, and it answers my question somewhat.

Unfortunately, it seems that <cut> has no var attribute. I'm new to JSTL, but for now, I can't see how to use <cut>'s escapeXml abilities to put the result into a VAR.

Also, I'm not sure <cut>'s escapeXml is the same as "encoding".

It would be interesting to get a String like "http://a.b.com?one two three" encoded using a JSTL tag, and into a variable using the var attribute.

Actually, though, I can't figure out how to do this. Seems like <c:url> does it, but by then, it's "too late"; we've already accessed a URL. It would be nice to do the encoding for the sole purpose of getting the encoded thing into a variable.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, if we don't use <c:param>, <c:url> will do URL rewriting (adding session info to the URL), but will NOT do URL encoding.

How did you deduce this statement ? Did you read it somewhere, or did you try it ?

I tried with Tomcat 5.5, and both rewriting and encoding are performed, even with parameters.
 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I haven't had time to try this yet.

But the Head First Servlets and JSP book, page 466, says it pretty emphatically, that <c:url> does URL rewriting, but not URL encoding... then <c:params> to the rescue for URL encoding.

I'm really curious about why they say this (in the book), and why anyone would WANT to do URL rewriting but NOT URL encoding.


 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

But the Head First Servlets and JSP book, page 466, says it pretty emphatically, that <c:url> does URL rewriting, but not URL encoding... then <c:params> to the rescue for URL encoding.

Well it makes sense doesn't it? The url you put in the <c:url> shouldn't ever need to be encoded as they are not allowed any special characters, check domain name rules.
The parameters that you add on may have spaces, special characters etc so that is why the <c:params> are encoded.

You may just use <c:url> if you are just wanting to write an absolute hyperlink as it references from the root of the application.

Sean
 
Kay Li
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to Chatles Lyons' book on SCWCD 5, <c:url> does URL rewriting and URL encoding, but not to request parameters added for GET method.
So <c:params> comes to the rescue for request parameters encoding.

like:
<c:set var=”last” value=”Hidden Cursor” />
<c:set var=”fi rst” value=”Crouching Pixels”/>
<c:url value=”/inputComments.jsp?fi rst=${fi rst}&last=${last}” var=”inputURL” />
The URL using params is: ${inputURL} <br>


The URL using params is: /myApp/inputComments.
jsp?fi rst=Crouching Pixels&last=Hidden Cursor


So you need do this:

<c:url value=”/inputComments.jsp” var=”inputURL” >
<c:param name=”fi rstName” value=”${fi rst}” />
<c:param name=”lastName” value=”${last}” />
</c:url>

Kay
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic