Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I send a POST form in an encoding other than of its body?
    text
    copied!<p>I have an HTML page that looks like:</p> <pre><code>&lt;HTML&gt; &lt;meta http-equiv='Content-Type' content='text/html; charset=gb2312'&gt; &lt;BODY onload='document.forms[0].submit();'&gt; &lt;form name="form" method="post" action="/path/to/some/servlet"&gt; &lt;input type="hidden" name="username" value="麗安"&gt; &lt;!-- UTF-8 characters --&gt; &lt;/form&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre> <p>As you can see, the content of this page is UTF-8, but I need to send it with GB2312 character encoding, as the servlet that I am sending this page to expects from me GB2312.</p> <p>Is this a valid scenario? Because in the servlet, I couldn't retrieve these Chinese characters back using a filter that sets the character encoding to GB2312!</p> <p>I've created a sample Servlet:</p> <pre><code>package org.daz; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class EncodingServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String ENCODING = "GB2312"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { setCharacterEncoding(request, response); String username = request.getParameter("username"); System.out.println(username); } private void setCharacterEncoding(HttpServletRequest request, HttpServletResponse response)throws IOException{ request.setCharacterEncoding(ENCODING); response.setCharacterEncoding(ENCODING); } } </code></pre> <p>The output is: <code>楹��</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload