0
var name = "Świat";

jQuery.ajax({
  async: false,
  contentType: "application/x-www-form-urlencoded;charset=UTF-8",
  type: 'POST',
  url: "test.do?name="+encodeURIComponent(name),
  noCache:true,
  data:{
  },
  beforeSend: function(xhr) {
    xhr.setRequestHeader("Accept-Charset","UTF-8");
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
...

in java i will have value like :

String pName = request.getParameter("name");

pName = "Å?wiat";

Now i want to search record from database using this name but in db has value with "Świat"

How can i find this or decode this ?

hsuk
  • 6,770
  • 13
  • 50
  • 80
Ruchi
  • 5,032
  • 13
  • 59
  • 84
  • Have you tried to urlDEcode the string in java? e.g. with this: http://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLDecoder.html#URLDecoder() ? – simplyray Jan 17 '13 at 07:57
  • how your form looks like ? – insomiac Jan 17 '13 at 08:01
  • And possibly `script charset="UTF-8"`. – Joop Eggen Jan 17 '13 at 08:02
  • i have used URLDecode also and it will give same input...in form have also page encoding UTF-8 – Ruchi Jan 17 '13 at 08:11
  • What is the encoding of the script file on the filesystem? Perhaps it is not UTF-8 and that is the problem? – theadam Jan 17 '13 at 08:20
  • might be a duplicate [1](http://stackoverflow.com/questions/4841331/url-encode-and-decode-special-character-in-java) [2](http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java) – linski Jan 17 '13 at 09:22

2 Answers2

0

A very common task when working with Webservices, is to encode/decode specific URI components, and for there is no direct API in Java for the same, it gets a difficult. Below is the code piece that I have been using for quite some time now.

Hope this helps,

public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";

 public static String encodeURIComponent(String input) {
  if(StringUtils.isEmpty(input)) {
   return input;
  }

  int l = input.length();
  StringBuilder o = new StringBuilder(l * 3);
  try {
   for (int i = 0; i < l; i++) {
    String e = input.substring(i, i + 1);
    if (ALLOWED_CHARS.indexOf(e) == -1) {
     byte[] b = e.getBytes("utf-8");
     o.append(getHex(b));
     continue;
    }
    o.append(e);
   }
   return o.toString();
  } catch(UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return input;
 }

 private static String getHex(byte buf[]) {
  StringBuilder o = new StringBuilder(buf.length * 3);
  for (int i = 0; i < buf.length; i++) {
   int n = (int) buf[i] & 0xff;
   o.append("%");
   if (n < 0x10) {
    o.append("0");
   }
   o.append(Long.toString(n, 16).toUpperCase());
  }
  return o.toString();
 }

 public static String decodeURIComponent(String encodedURI) {
  char actualChar;

  StringBuffer buffer = new StringBuffer();

  int bytePattern, sumb = 0;

  for (int i = 0, more = -1; i < encodedURI.length(); i++) {
   actualChar = encodedURI.charAt(i);

   switch (actualChar) {
    case '%': {
     actualChar = encodedURI.charAt(++i);
     int hb = (Character.isDigit(actualChar) ? actualChar - '0'
       : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
     actualChar = encodedURI.charAt(++i);
     int lb = (Character.isDigit(actualChar) ? actualChar - '0'
       : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
     bytePattern = (hb << 4) | lb;
     break;
    }
    case '+': {
     bytePattern = ' ';
     break;
    }
    default: {
     bytePattern = actualChar;
    }
   }

   if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
    sumb = (sumb << 6) | (bytePattern & 0x3f);
    if (--more == 0)
     buffer.append((char) sumb);
   } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
    buffer.append((char) bytePattern);
   } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
    sumb = bytePattern & 0x1f;
    more = 1;
   } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
    sumb = bytePattern & 0x0f;
    more = 2;
   } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
    sumb = bytePattern & 0x07;
    more = 3;
   } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
    sumb = bytePattern & 0x03;
    more = 4;
   } else { // 1111110x
    sumb = bytePattern & 0x01;
    more = 5;
   }
  }
  return buffer.toString();
 }
vajrakumar
  • 758
  • 2
  • 7
  • 21
  • there IS a Java API for this see [URLDecoder](http://docs.oracle.com/javase/6/docs/api/java/net/URLDecoder.html) [URLEncoder](http://docs.oracle.com/javase/6/docs/api/java/net/URLEncoder.html) – linski Jan 17 '13 at 08:56
0

just need to encode String from ISO to UTF and i will get records. like

name = new String(name.trim().getBytes("ISO-8859-1"),"UTF-8");
Ruchi
  • 5,032
  • 13
  • 59
  • 84