code: public static String encodeString(String string,String charset)
      {
          if (charset==null)
              charset=StringUtil.__ISO_8859_1;
          byte[] bytes=null;
          try
          {
              bytes=string.getBytes(charset);
          }
          catch(UnsupportedEncodingException e)
          {
              log.warn(LogSupport.EXCEPTION,e);
              bytes=string.getBytes();
          }
          
          int len=bytes.length;
          byte[] encoded= new byte[bytes.length*3];
          int n=0;
          boolean noEncode=true;
          
          for (int i=0;i<len;i++)
          {
              byte b = bytes[i];
              
              if (b==' ')
              {
                  noEncode=false;
                  encoded[n++]=(byte)'+';
              }
              else if (b>='a' && b<='z' ||
                       b>='A' && b<='Z' ||
                       b>='0' && b<='9')
              {
                  encoded[n++]=b;
              }
              else
              {
                  noEncode=false;
                  encoded[n++]=(byte)'%';
                  byte nibble= (byte) ((b&0xf0)>>4);
                  if (nibble>=10)
                      encoded[n++]=(byte)('A'+nibble-10);
                  else
                      encoded[n++]=(byte)('0'+nibble);
                  nibble= (byte) (b&0xf);
                  if (nibble>=10)
                      encoded[n++]=(byte)('A'+nibble-10);
                  else
                      encoded[n++]=(byte)('0'+nibble);
              }
          }
  
          if (noEncode)
              return string;
          
          try
          {    
              return new String(encoded,0,n,charset);
          }
          catch(UnsupportedEncodingException e)
          {
              log.warn(LogSupport.EXCEPTION,e);
              return new String(encoded,0,n);
          }
      }
docs: Perform URL encoding.
  @param string
  @return encoded string.