Meaningless Drivel is fun!*
The moose likes JSP and the fly likes URLEncoding Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "URLEncoding Issue" Watch "URLEncoding Issue" New topic
Author

URLEncoding Issue

Sam Kumar
Ranch Hand

Joined: Nov 30, 2001
Posts: 38
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
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.


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
 
 
subject: URLEncoding Issue
 
Similar Threads
Tomcat 6 no parsing request parameters with &amp;
Ajax Problem : passing html:select element in returnstring
URLEncoding Issue
URLEncoding Issue
Escaping Single Quote (apostrophe )