Some questions about URLEncoder and URLDecoder?

牧云@^-^@ 提交于 2020-01-01 19:01:58

问题


I have gone thru javadocs of URLEncoder and URLDecoder. Then got more inquisitive. Consider the server as tomcat.In any webapplication whenever we submit the form , server converts the forms fields into urlencoded fields and when we do request.getParamter("fieldName"). Server again decode it with URLDecoder. Is that correct? Does server do it or browser? Simlary when we type any url in address bar same stuff happens? If server or browser does that when we require to encode or decode the request paramter explicitly with the help of URLEncoder and URLDecoder? Though these are basic questions but could not find these anwers clearly.


回答1:


In any webapplication whenever we submit the form , server converts the forms fields into urlencoded fields and when we do request.getParamter("fieldName").

No. The browser does that.

Server again decode it with URLDecoder. Is that correct?

Yes.

Does server do it or browser?

The browser.

If server or browser does that when we require to encode or decode the request parameter explicitly with the help of URLEncoder and URLDecoder?'

I don't know what that means but it's still the browser. You only need to encode the request parameters if you are sending the request from application code. You don't need to decode them at all if you're running in a servlet container: it will do that for you.




回答2:


While it is true that browser does encode a URL before passing it off to the web server but there may not be a browser involvement all the time.

e.g. your server app might be making a REST based call and passing some data in a simple GET request. And then if you don't encode it on your server it may become garbled when receiver decodes it.

Therefore it is highly recommended to always encode the URL before sending it off in your server code.



来源:https://stackoverflow.com/questions/10846901/some-questions-about-urlencoder-and-urldecoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!