servlet页面中文乱码或是问号

 response.setContentType("text/html");
      response.setCharacterEncoding("utf-8");  把这句加上,且重启Tomcat即可,页面刷新即可。要重启Tomcat才行。
网址: http://localhost:8080/HelloForm?first_name=ffffff&last_name=ddd即看到修改后的效果

 
</pre><pre name="code" class="java">// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// 扩展 HttpServlet 类
public class HelloForm extends HttpServlet {
 
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // 设置响应内容类型
      response.setContentType("text/html");
      response.setCharacterEncoding("utf-8");  把这句加上,且重启Tomcat即可,页面刷新即可。要重启Tomcat才行。

      PrintWriter out = response.getWriter();
	  String title = "使用 GET 方法读取表单数据";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType +
                "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" +
                "<h1 align=\"center\">" + title + "</h1>\n" +
                "<ul>\n" +
                "  <li><b>名字</b>:"
                + request.getParameter("first_name") + "\n" +
                "  <li><b>姓氏</b>:"
                + request.getParameter("last_name") + "\n" +
                "</ul>\n" +
                "</body></html>");

  }
}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页