1

Is there a way to set response encoding in embedded tomcat? By far I could only find setting URI encoding. But I need to set the response encoding.

The surprise is, standalone tomcat is by default sending data in UTF-8.

Venkata Jaswanth
  • 1,661
  • 2
  • 11
  • 12
  • just use setCharacterEncoding method of ServletResponse class avaliable in HttpServletResponse to set response encoding if you dont want to use filter. – Kunal Surana Feb 15 '16 at 11:43

2 Answers2

0

You can create a filter to set your response encoding

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.*;  

public class MyFilter implements Filter{  

public void init(FilterConfig arg0) throws ServletException {}  

public void doFilter(ServletRequest req, ServletResponse resp,  
    FilterChain chain) throws IOException, ServletException {  

    resp.setCharacterEncoding("text/plain; charset=UTF-8"); 

    chain.doFilter(req, resp);//sends request to next resource  


    }  
public void destroy() {}  
}  
Kunal Surana
  • 659
  • 5
  • 14
0
  1. Create filter:

    `package charsetFilter.classes;

     import java.io.IOException;
     import javax.servlet.Filter;
     import javax.servlet.FilterChain;
     import javax.servlet.FilterConfig;
     import javax.servlet.ServletException;
     import javax.servlet.ServletRequest;
     import javax.servlet.ServletResponse;
    
    public class CharsetFilter implements Filter{
        private String encoding;
    
        public void init(FilterConfig config) throws ServletException{
                encoding = config.getInitParameter("requestEncoding");
                if( encoding==null ) encoding="UTF-8";
        }
    
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain       next)
        throws IOException, ServletException{
            // Respect the client-specified character encoding
            // (see HTTP specification section 3.4.1)
                if(null == request.getCharacterEncoding())
                request.setCharacterEncoding(encoding);
                /**
            * Set the default response content type and encoding
            */
            response.setContentType("text/html; charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
                next.doFilter(request, response);
        }
    
            public void destroy(){}
    }`
    
  2. Add this filter in web.xml as:

    `<filter>
            <filter-name>CharsetFilter</filter-name>
            <filter-class>charsetFilter.classes.CharsetFilter</filter-class>
                <init-param>
                    <param-name>requestEncoding</param-name>
                    <param-value>UTF-8</param-value>
                </init-param>
    </filter>
    
    <filter-mapping>
            <filter-name>CharsetFilter</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>`
    
  3. HTML-meta tags should be written in all HTML files:
    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"> <head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />

  4. Set your servlet as:
    request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8");

  5. Deal with servlet requests as:
    String input = new String(request.getParameter("foo").getBytes("iso-8859-1"), "utf-8"); String input = URLDecoder.decode(request.getParameter("keyWord"), "UTF-8");

Let me know if you still have problems

Ghayel
  • 1,113
  • 2
  • 10
  • 19