views:

99

answers:

1

The Question is,

In a regular HTTP Request to a server (non-ajax), Is the Query String passed by GET method to some server, get affected by the encoding specified by this :

<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>

If the answer is no, How to define the encoding schema for the parameters of GET method ?

example: If I have a html that looks:

<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
</head>
<body>
 <form method="GET" action="/some/server">
  <input type="text" name="name1" value="someNon-ASCII_value1" />
  <input type="submit" />
 </form>
</body>
<html>

When the user click the submit button, what will be the encoding of the value someNon-ASCII_value1 ?

+3  A: 

The browser will use the same encoding as it was been instructed to use to display the page with the form. This can indeed be the encoding as specified in the meta content-type entry in the HTML head, but this can be overridden by a content-type header in the HTTP response.

You however also need to take into account the correct URL encoding in the server side to decode the GET parameters. It's unclear which server you're using, but in case of for example Tomcat you need to set the URIEncoding attribute of the <Connector> element to the same encoding.

<Connector ... URIEncoding="gb2312">

Also see this article for more background information.

BalusC
Thanks. I ensured that the encoding in the `content` attribute of the `meta` tag is what is being used to encode the query string of the get request by running simple Servlet example.
Mohammed
You're welcome.
BalusC
I am sorry, but Is there any way for the JAVA server to know the character encoding of the client ? (i.e. through some request header ?? )
Mohammed
Yes, [ServletRequest#getCharacterEncoding()](http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletRequest.html#getCharacterEncoding%28%29).
BalusC
from javadoc : "Returns the name of the character encoding used in the body of this request", but as you know GET request doesn't have bodies, just header.I tried it and of course returns `null` !BTW, I hope if I know the scenarios where getCharacterEncoding doesn't returns null!!!!
Mohammed
It's indeed not `null` if you use `POST`. For GET you're dependent on the response encoding which you specified yourself in the initial page and the URI encoding which you specified in the server configuration (both which should be the same).
BalusC
About GET; So, I should be the both entities for me to know the encoding of the coming request? But what if I expect some request from external entity that says "expect from me `GB2312` unless I send in something different". So how can I know what will be that `something different` as If I were him, I'll consider the encoding specified in the `meta` tag is that `something` different??
Mohammed
If you intend to expose your API for external use, then you should document the encoding accordingly that it's clear for the 3rd party which to use. At least, the webbrowsers just adheres it. For internal use, it's of course clear enough which one to use.
BalusC
So, Noway.Thanks too much. I cannot tell you how I am thoughtful to you.Thanks for your time and your patient.BTW, I get happy once see you replying my dump Questions :)
Mohammed