1,以Post方式提交包含中文字符的表單
對于servlet加上:
request.setCharacterEncoding("GBK");
response.setContentType("text/html,charset=GBK");
對于jsp來說:
<%@ page=contentType="text/html;charset=GBK"%>
<% request.setCharacterEnconding="GBK" %>
2,以get方式提交包含中文字符的表單
get方式中的中文字符是包含在URL中的,不屬于http請求的正文部分,采用setCharacterEncoding()的方式是沒有作用的,需要用以下代碼,將字符串按照指定的編碼方式進行重新分配。
String param=request.getParameter("param");
param=new String (param.getBytes(),"GBK");
3,jsp文件的編碼問題:
<%@ page pageEncoding="UTF-8" %>
4,使用過濾器解決中文亂碼問題
如果每個jsp和servlet都加上上述的處理代碼,就會顯得冗余,使用過濾器就可以達到這樣的效果:
doFilter{
if(request.getCharacterEncoding()==null){
request .setCharacterEncoding("UTF-8");
}
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request,response);
}
過濾器配置:
web-xml:
主要的一步:
<url-pattern>/*</url-pattern>