servlet在網頁中中文顯示成亂碼“?”
阿新 • • 發佈:2021-03-07
servlet原始碼如下
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<!doctype html public \"-//w3c//dtd html 4.01 transition//CHN\">" );
out.println("<HTML>");
out.println(" <HEAD><title>夢幻麗莎髮廊</TITLE></HEAD>");
out.println(" <Body>");
out.println(" 天空掛著一輪紅月亮");
out.println(this.getClass());
out.println(" 我離開了夢幻麗莎髮廊" );
out.println(" </body>");
out.println("</html>");
out.flush();
out.close();
}
異常如下
解決方法
將response.setContentType("text/html")
修改為
response.setContentType("text/html;charset=gbk");