Search code examples
javaservletsutf-8url-encoding

Java : When a parameter has a value that matches a UTF-8 encoded string in a java HTTPRequestServlet, a doGet decodes it by default


I am new to servlets. I issue a GET request on a servlet with a parameter say param=https%3A%2F%2Fwww.somesite.com from the client side. On the server side, there is a doGet method that takes HTTPServletRequest and HTTPServletResponse object. When i try to retrieve param, it gives me https://www.somesite.com instead of https%3A%2F%2Fwww.somesite.com.

The server side code runs on Websphere. Is it an expected behavior or is there any possible explanation for the same ?


Solution

  • This is the correct behaviour exmplined in qoutes you put as your question's subject.

    URL is restricted for using some characters. You can only use English letters, digits and some special characters. If you have to send other symbols they have to be encoded using the % notation. For example %20 means space, %3A means colon etc. The encoding is done by client. For convenience servlet API decodes servlet parameters automatically.

    This is what you read in documentation and see in action.