解决方法是在response.getWriter()前加上response..setContentType("text/html; charset=UTF-8");html
下面是示例代码,注意代码中的红色字体:session
protected void doGet(HttpServletRequest req, HttpServletResponse resp)jsp
throws ServletException, IOException {字体
req.setCharacterEncoding("utf-8");spa
User user=(User)req.getSession().getAttribute("user");htm
if(null==user){utf-8
req.getRequestDispatcher("session/userLogin.jsp").forward(req, resp);get
//避免恶意用户直接在地址栏访问localhost:8080/test/UpdateServletit
return;io
}
//!resp.setCharacterEncoding("utf-8");单纯的这么设置同样有乱码
resp.setContentType("text/html; charset=UTF-8");
PrintWriter out=resp.getWriter();
if("admin".equals(user.getAuthority())){
out.print("成功");
}else{
out.print("无权限");
}
}