请按照以下要求设计一个向页面输出中文的类ChineseServlet。要求如下:
(1)创建一个ChineseServlet类,使其继承HttpServlet类并重写该类的doGet()方法。
(2)在doGet()方法中,使用HttpServletResponse对象的setContentType()方法将浏览器编码改为utf-8。
(3)使用HttpServletResponse对象获取输出流,向浏览器输出“JavaWeb程序设计任务教程”的中文字样。
正确答案:
@WebServlet(name = "ChineseServlet",urlPatterns="ChineseServlet")
public class ChineseServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String data="JavaWeb程序设计任务教程";
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(data);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException {
doGet(request,response);
}
}