Servlet的response输出到页面时乱码的解决方法
1 package com.mhb; 2 import java.io.IOException; 3 import java.io.PrintWriter; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 11 public class MyServlet extends HttpServlet { 12 13 public void init() throws ServletException { 14 } 15 16 public void doGet(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 //解决乱码方法一:response.setCharacterEncoding("UTF-8");此种方法无效的时候请尝试下一行(19行)的方法。
19 response.setHeader("content-type","text/html;charset=UTF-8");
20 //获得输出流 21 PrintWriter out = response.getWriter(); 22 //输出到网页上 23 out.print("<font color='red'>这是我的第一个示例</font><br>testtest"); 24 } 25 26 public void doPost(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 } 29 30 public void destroy() { 31 super.destroy(); 32 } 33 }