1ãæµè§å¨è°ç¨jspï¼htmlç页é¢ä¸ææ¾ç¤ºä¹±ç
æ¤æ
åµé满足两个è¦æ±ï¼
ï¼1ï¼æ件æ¬èº«æ¯ä»¥utf-8ç¼è¾ä¿åçï¼myEclipseä¸å¨propertiesä¸é¼ æ å³é®éæ©utf-8ï¼
ï¼2ï¼æµè§å¨ç¨utf-8解æï¼
(æå¨)==> å¨æµè§å¨ä¸å³é®éæ©ç¼ç æ ¼å¼ä¸ºutf-8
(æºè½)==> å¨æ件ä¸åå
¥å¦ï¼ <meta name="content-type" content="text/html; charset=UTF-8"> éè¿<meta>æ ç¾æ¨¡æresponse头ï¼èµ·å°åè¯æµè§å¨ç¨utf-8çç¼ç 解æ
(æºè½)==> response.setContentType("text/html;charset=UTF-8");èµ·å°åè¯æµè§å¨ç¨utf-8çç¼ç 解æ
常ç¨ï¼
<meta name="content-type" content="text/html; charset=UTF-8">æ<meta charset="utf-8">
<%@ pageEncoding="utf-8"%>
<?xml encoding="UTF-8"?>
2ãéè¿æµè§å¨è°ç¨servletï¼é¡µé¢æ¾ç¤ºä¹±ç ã
Servletä¹±ç å为requestä¹±ç åresponseä¹±ç ï¼
ï¼1ï¼responseä¹±ç é®é¢
解å³æ¹æ³ï¼
å¨ç½ä¸å¾ææç解å³æ¹æ³æ¯æ·»å ï¼
response.setCharacterEncoding("UTF-8");
解å³ä¸äºï¼åæ¥åæå°ä¸æ¡è§£å³æ¹æ³æ¯ï¼
response.setContentType("text/html;charset=utf-8");æè
response.setHeader("content-type","text/html;charset=UTF-8");åè¯æµè§å¨ç¨utf-8解æã(setHeaderæ¯HttpServletResponseçæ¹æ³ãå¦ææ³å¨æ¦æªå¨Filterä¸è®¾ç½®å符ç¼ç ï¼åæ æ¤æ¹æ³ï¼å 为FilterçdoFilteræ¹æ³çåæ°ç±»åæ¯ServletResponse)
两å¥é½å¡«ä¸ï¼åæ¥ç»äºè§£å³äºè¿ä¸ªé®é¢ï¼
å
¶å®æ们åºè¯¥æèä¸ä¸æ¬è´¨ï¼
response.setContentType("text/html;charset=UTF-8"); ç®çæ¯ä¸ºäºæ§å¶æµè§å¨çè¡ä¸ºï¼å³æ§å¶æµè§å¨ç¨UTF-8è¿è¡è§£ç ï¼
response.setCharacterEncoding("UTF-8");ç®çæ¯ç¨äºresponse.getWriter()è¾åºçå符æµçä¹±ç é®é¢ãå¦ææ¯response.getOutputStream()æ¯ä¸éè¦æ¤ç§è§£å³æ¹æ¡çï¼å 为è¿å¥è¯çæææ¯ä¸ºäºå°response对象ä¸çæ°æ®ä»¥UTF-8解ç åçåèæµååæµè§å¨ï¼
温馨提示:答案为网友推荐,仅供参考