Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLEncoding Issue

 
Sam Kumar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am facing one problem related to URLEncoding. In my JSP after retrieving lets say name from database...i am encoding using the URLEnoder class and generate a dynamic HTML but it seems that URLCoding is not working properly. Here is the code snippet:
String testStr = "Test'String'";
testStr = URLEncoder.encode( testStr );
System.out.println("encodedValue: " + testStr);
I verified that endoded value is:
Test%27sString
Now I am generating dynamic HTML with
<A HREF="javascript:submitForm('"<%= testStr%>"');">Test<A>
The generated page does not contains the encoded value, because of single quote, this link is not working:
<A HREF="javascript:submitForm('Test'String');">Test</A>
Ideally I shoould see:
<A HREF="javascript:submitForm('Test%27sString');">Test</A>
Thanks in advance!
SK
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Kumar:
Ideally I shoould see:
<A HREF="javascript:submitForm('Test%27sString');">Test</A>

Why do you want the string to be URL-encoded? I can't remember having any trouble using unencoded URLs in JavaScript functions. On the other hand, I believe you should encode the single quote with a backslash instead of URL-encoding.
I might be wrong though, as it's been a while since I have dealt with JavaScript.
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Kumar:
Now I am generating dynamic HTML with
<A HREF="javascript:submitForm('"<%= testStr%>"');">Test<A>

I think you should try a different quote-escaping scheme... URL-encoding may not be the issue at all.
You are literally getting,
<A HREF="javascript:submitForm('"Test'String"');">
and this is not what you want (count the single quote literals, and you'll see what I mean).
Try this instead,

I haven't tested my suggestion, but I suspect that the quoting characters is the source of the problem.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic