1

Using jsp we print this Hh’k value in hidden field and then submit it. Then in servlet, we are getting it as parameter Hh'k, instead we want this as Hh’k.

Any suggestions?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
harishtps
  • 1,439
  • 7
  • 20
  • 35
  • Please note that `’` represents a right single quotation mark `’`, not a simple apostrophe `'` as you've in your parameter value example. – BalusC Sep 19 '11 at 20:21

1 Answers1

2

The browser don't do that because there's no reason. It just URL-encodes the special characters conform the application/www-x-form-urlencoded contract which is automatically URL-decoded by calling getParameter().

If you really need to XML-escape them, you'd need to do it yourself after obtaining the request parameter. The Apache Commons Lang StringEscapeUtils#escapeXml() is helpful in this:

String foo = request.getParameter("foo");
String escapedFoo = StringEscapeUtils.escapeXml(foo);
// ...

However, why would you do that? Do you have problems with redisplaying them in the HTML? For that there's a much easier solution, just use UTF-8 everywhere. E.g. add the following in top of your JSP:

<%@page pageEncoding="UTF-8" %>

Etcetera.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • There is no problem displaying it in the HTML, we have to maintain the same asscii charater while sending it another server. – harishtps Sep 19 '11 at 19:56
  • I have tried StringEscapeUtils.escapeXml but its not working :( – harishtps Sep 19 '11 at 19:56
  • Ah well, then go ahead escaping them yourself. Maybe you want to implement it in the API which is sending it to the other server instead of doing it for every inpue. Or perhaps you need to review the API which is doing the sending job if it's doing the job correctly. Does it take place by HTTP? Do you URL-encode the parameters using `URLEncoder` and so on? – BalusC Sep 19 '11 at 19:57
  • How so not working? Please elaborate in more detail. I am unfortunately not immediately able to fly/beam over to you to see any error messages or unexpected results with my own eyes. – BalusC Sep 19 '11 at 19:58
  • Actually the flow is, in jsp page we will get that "Hh’k" value and then will be submitted to a servlet and then write to a file. The value is seen in jsp page and after submission in servlet it is not seen as "Hh’k" instead as "Hh'k". We want that exact code so that we can write to a file. – harishtps Sep 19 '11 at 20:15
  • Use `StringEscapeUtils`. The `System.out.println(StringEscapeUtils.escapeXml("Hh’k"));` gives here `Hh’k`, so I don't see any problems. In your question you have by the way a simple apostrophe (perhaps by typing mistake instead of literally copypasting?), which would be XML-escaped as `'`. After all, it's still beyond me why you would it like that in the file. I still believe that you're looking for a solution for a certain and unclear problem in the wrong direction. – BalusC Sep 19 '11 at 20:19
  • Oh, are you writing data to file as UTF-8 using `new OutputStreamWriter(output, "UTF-8")` and reading from it as UTF-8 by `new InputStreamReader(input, "UTF-8")`? Perhaps you aren't at all which would (incorreclty) force you to use XML entities. See also the "use UTF-8 everywhere" link in my answer. – BalusC Sep 19 '11 at 20:30