  | Mailing List | | Home | | Forum Home | | JBoss - Java Application Server | | Tomcat - JSP/Servlet container | | Struts - A MVC web framework | | iText - An open source PDF Java Library | | JDOM - JDOM XML Parser | | JSP - A mailing list about Java Server Pages specification and reference | | J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition | | J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog | | Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology | |
Struts & Hibernate
|
|
|
  | | | setCharacterEncoding for request/response objects | setCharacterEncoding for request/response objects 2004-09-28 - By Peter Claesson
Back I am struggling with a localization issue where I want to set the encoding of both the request and response objects at run-time. I have an applicatoin consisting of 17 JSP pages that now must support Thai. I'm unable to use UTF8 due to the fact that I need to pass the data over to a windows server, which only support 8-bit characters. I'm using ServletExec 5.0 on Windows.
The encoding I'm trying to use is windows-874 (See http://ows-874.ora-code.com). I've set both the request and response objects using setCharacterEncoding, but the page returned to the browser is still ISO8859-1.
I have also tried calling response.setContentType("text/html; charset=windows-874 (See http://ows-874.ora-code.com)"); with the same result. Carefully reading the the API descriptions for getCharacterEncoding, getContentType, setCharacterEncoding, and setContentType it appears that both setCharacterEncoding and setContentType must be called. I've tried that with the same result. The page is always returned to the browser with ISO8859-1 encoding.
I even read the encoding (in my filter) before I set it and after it is set.
Here is a copy of the doFilter method. The "encode" variable is set in init method to value windows-874 (See http://ows-874.ora-code.com).
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { Debug.println("HtmlcEncodingFilter.doFilter(" + request + ") : " + encode); HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; Debug.println("BEF: request.getCharacterEncoding = " + req.getCharacterEncoding()); Debug.println("BEF: response.getContentType = " + res.getContentType()); Debug.println("BEF: response.getCharacterEncoding = " + res.getCharacterEncoding()); req.setCharacterEncoding(encode); res.setCharacterEncoding(encode); res.setContentType("text/html;charset=" + encode); Debug.println("AFT: request.getCharacterEncoding = " + req.getCharacterEncoding()); Debug.println("AFT: response.getContentType = " + res.getContentType()); Debug.println("AFT: response.getCharacterEncoding = " + res.getCharacterEncoding()); chain.doFilter(request, response); // chain filter, if needed return; }
This is what the SE log shows.
[Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM HtmlcEncodingFilter.doFilter(com.newatlanta.servletexec.Request@(protected)) : windows-874 (See http://ows-874.ora-code.com) [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM BEF: request.getCharacterEncoding = null [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM BEF: response.getContentType = null [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM BEF: response.getCharacterEncoding = iso-8859 (See http://iso-8859.ora-code.com)-1 [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM AFT: request.getCharacterEncoding = null [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM AFT: response.getContentType = text/html;charset=windows-874 (See http://ows-874.ora-code.com) [Wed Sep 22 08:15:53 PDT 2004] 9/22/04 8:15:53 AM AFT: response.getCharacterEncoding = windows-874 (See http://ows-874.ora-code.com)
The only way I can get the request and response objects to use windows-874 (See http://ows-874.ora-code.com) is by using a page directive <%@ page contentType = "text/html; charset=windows-874 (See http://ows-874.ora-code.com)" %> in each JSP page. As we do not want to maintain hardcoded values, I'm desperate of getting this to work.
Am I doing something wrong? or is there a bug?
I'm grateful for any help or pointers.
/Peter
__ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ __ To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
|
|
 |