方案一:
public class ResoponseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置服务器字符集为utf-8
resp.setCharacterEncoding("utf-8");
//通过响应头。设置浏览器也使用utf-8
resp.setHeader("Content-type","text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("超哥哥");
}
}
方案二:
public class ResoponseServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*//设置服务器字符集为utf-8
resp.setCharacterEncoding("utf-8");
//通过响应头。设置浏览器也使用utf-8
resp.setHeader("Content-type","text/html;charset=UTF-8");
*/
//他会同时设置服务器和浏览器客户端都使用utf-8字符集,还设置了响应头
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
writer.write("超哥哥");
}
}