go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Java Server Pages (JSP) » Globalize JSP web application by charset encoding
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: Globalize JSP web application by charset encoding
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 11/04/2009 01:54:05 AM    Edit  |   Quote  |   Report 
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.


  •  Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 11/04/2009 02:11:46 AM    Edit  |   Quote  |   Report 
    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.

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 11/04/2009 02:19:23 AM    Edit  |   Quote  |   Report 
    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");
    


     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 11/04/2009 02:38:11 AM    Edit  |   Quote  |   Report 
    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>");
    
    }
    


     Profile | Reply Points Earned: 0

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