Servlet蛋疼的编码问题
servlet都说设置了request.setCharacterEncoding("UTF-8")编码就不会出错(utf-8 指所用的编码方法)接受request参数的变量还得再者之前设置
例如:
1 String title; 2 String content; 3 try { 4 request.setCharacterEncoding("UTF-8"); 5 6 title=request.getParameter("title"); 7 content=request.getParameter("content"); 8 9 } catch (UnsupportedEncodingException e1) { 10 // TODO Auto-generated catch block 11 e1.printStackTrace(); 12 }
此时将不会出现乱码
但是
try { request.setCharacterEncoding("UTF-8"); String title=request.getParameter("title"); String content=request.getParameter("content"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
这样便会出现乱码