We help IT Professionals succeed at work.

submitting special characters in html form

greenearth
greenearth asked
on
Medium Priority
363 Views
Last Modified: 2012-05-12
I have  base64 string which i submit using an html form using text box. it has special characters like / , + etc. when i submit the string i receive on the backend here java it gets converted to something like %2F for / . how can avoid this and get the actual value
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You can decode it if necessary, but how exactly are you receiving and reading it? Normally it shouldn't need decoding
Awarded 2011
Awarded 2011

Commented:
Awarded 2011
Awarded 2011

Commented:


This is waht URLDecoder will do:
The following rules are applied in the conversion:

    The alphanumeric characters "a" through "z", "A" through "Z" and "0" through "9" remain the same.
    The special characters ".", "-", "*", and "_" remain the same.
    The plus sign "+" is converted into a space character " " .
    A sequence of the form "%xy" will be treated as representing a byte where xy is the two-digit hexadecimal representation of the 8 bits. Then, all substrings that contain one or more of these byte sequences consecutively will be replaced by the character(s) whose encoding would result in those consecutive bytes. The encoding scheme used to decode these characters may be specified, or if unspecified, the default encoding of the platform will be used
Awarded 2011
Awarded 2011

Commented:
This is example:
String shtml = "http://search.barnesandnoble.com/booksearch/first%20book.pdf";

        System.out.println(URLDecoder.decode(shtml));
        

Open in new window



Output:

http://search.barnesandnoble.com/booksearch/first book.pdf

Open in new window

Awarded 2011
Awarded 2011

Commented:
this is better as it will not write deprecated warning:

            try{
String shtml = "http://search.barnesandnoble.com/booksearch/first%20book.pdf";

        System.out.println(URLDecoder.decode(shtml, "UTF-8"));
            }catch(Exception ex){
                ex.printStackTrace();
            }

Open in new window


http://search.barnesandnoble.com/booksearch/first book.pdf

Open in new window

Awarded 2011
Awarded 2011
Commented:

This is a little bit more complicated input with %2F which you mentioned, etc.:
            try{

              String shtml = "http%3A%2F%2Fsearch.barnesandnoble.com%2Fbooksearch%2Ffirst+book.pdf";
        System.out.println(URLDecoder.decode(shtml, "UTF-8"));
            }catch(Exception ex){
                ex.printStackTrace();
            }
        

Open in new window


http://search.barnesandnoble.com/booksearch/first book.pdf

Open in new window

CERTIFIED EXPERT
Top Expert 2016

Commented:
The proper way to do this would normally be the following

a. *en*code the parameter (if sending manually - otherwise the browser will do it)
b.  treat it as a normal string at the server end (which will decode it usually)

Author

Commented:
I  have not tried the solution. but i am accepting this