用MyEclipse 新建了Web Project,然后建立了一个Servlet,在doGet方法中谢了简单的一句欢迎语句,本来以为平平常常的事情,但是却出错了,乱码问题,在网上找了一些帖子,说是设置编码:
response.setCharacterEncoding("UTF-8"),但是我这里还是不起作用,然后又设置了Header,还是出错,最后将这两句话放在PrintWriter的前面,问题就解决了!
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
//解决不了,后来又搜到一条解决方法是:
response.setHeader("content-type","text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String sName = "";
if(request.getParameterNames().hasMoreElements())
{
sName=request.getParameter("name");
} //response.setContentType("text/html;charset=utf-8"); out.println("<html><body>");
out.println("您好:"+sName+"!");
out.println("</body></html>"); out.close();
}