Author |
Topic: Globalize JSP web application by charset encoding |
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Globalize JSP web application by charset encoding |
In developing a global web application, you are likely to have to consider character encoding. The first thing you have to keep in mind is that there are two character encoding's that need to be addressed for JSP:
Request/response character encoding: The character encoding of the parameters sent, for example, from a form and character encoding of the web pages generated by JSP. JSP page encoding: The character encoding of the JSP itself.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Request/response character encoding |
<%@ page contentType="text/html; charset=UTF-8" %> JSP is dynamically generated at runtime and transfered to client via a stream. The parameter defined in contentType is used to determine the response charset for this stream encoding.
This parameter is also the one used in the client's broswer for charset encoding.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
JSP page encoding -- The character encoding of the JSP itself. |
JSP is dynamically compiled at runtime and becomes Java byte code. Java byte code stores strings in Unicode (UTF-8), and, therefore, conversion from JSP page encoding to Unicode is necessary at the time of compilation.
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="GB2312" %>
There may be cases where JSPs are written in one encoding (GB2312) but the output response in another encoding (UTF-8). In such cases, pageEncoding page directive can be used. One can still specify just the contentType if the charset is the same for both pageEncoding and contentType.
Also note that if the contentType attribute is missing, the value of the pageEncoding is used if provided.
To make the parameter configurable, the dynamic way to do this is as the follows:
response.setContentType("text/html; charset=UTF-8");
It should be noticed that this settings can override tht static one.
Just a note, the following seems to do the same job.
response.setCharacterEncoding("UTF-8");
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
JSP's Servlet Counterpart |
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
res.setContentType("text/html; charset=UTF-8");
PrintWriter out = res.getWriter();
out.println("<html><body>");
out.println("Your name is " + name );
out.println("</body></html>");
}
|
|
|
|
|
|
|
|