1
public class FeedUpdaterServlet extends HttpServlet { 
public void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 
    PrintWriter out = resp.getWriter(); 
    req.setCharacterEncoding("utf-8"); 
    resp.setLocale(Locale.TAIWAN); 
    resp.setContentType("text/html; charset=utf-8"); 
    resp.setCharacterEncoding("utf-8"); 
    resp.getWriter().println("Hello, [email protected]!"); 
      out.println("我是人"); //some chinese character 
    out.println(resp.getCharacterEncoding()); 
    out.flush(); 
    out.close(); 
    } 

} 

웹 XML응답 인코딩 (응답 인코딩을 변경할 수 없습니다)

<locale-encoding-mapping-list> 
     <locale-encoding-mapping> 
      <locale>zh_TW</locale> 
      <encoding>utf-8</encoding> 
     </locale-encoding-mapping> 
    </locale-encoding-mapping-list> 

출력 : 안녕하세요, 세계! @! ??? ISO-8859-1

응답의 인코딩을 변경할 수없는 것 같습니다. 무슨 일이 있습니까?

답변

3

ServletResponse.setContentType(..)에 대한 문서에서는 getWriter() 메서드를 호출 한 후에이 메서드를 호출해도 아무런 효과가 없다고 분명히 명시되어 있습니다.

이 트릭을 수행합니다 당신이 당신의 HTML 파일의 특정 섹션에이 응답을 embbed 어떻게

public void doGet(HttpServletRequest req, HttpServletResponse resp) 
     throws IOException { 
    resp.setContentType("text/html; charset=UTF-8"); 
    PrintWriter out = resp.getWriter(); 
    out.println("Hello, [email protected]!"); 
    out.println("我是人"); //some chinese character 
    out.println(resp.getCharacterEncoding()); 
    out.flush(); 
    out.close(); 
} 
+0

를? – InsaurraldeAP

관련 문제