1. 程式人生 > 其它 >servlet在網頁中中文顯示成亂碼“?”

servlet在網頁中中文顯示成亂碼“?”

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");

解決後

解決