go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  JSP's Servlet Counterpart
 
Subject: JSP's Servlet Counterpart
Author: WebSpider
In response to: JSP page encoding -- The character encoding of the JSP itself.
Posted on: 11/04/2009 02:38:11 AM

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>");

}



 

> On 11/04/2009 02:19:23 AM WebSpider wrote:

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");






References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.