后台servlet使用PrintWriter类向前台页面写内容的代码
向前台写内容有单行写入也有多行一块写入
resp.getWriter().write("time = "+date);//是单行向前台写内容
本例中使用多行一块写入的方式
public class FirstServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String date=new Date().toLocaleString();
//resp.getWriter().write("time = "+date);//单行向前台写内容
resp.setCharacterEncoding("utf-8");//第一种方式设置字符集
//resp.setContentType("text/html;charset=utf-8");//第二种方式设置字符集
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<!DOCTYOPE html>");
out.println("<html>");
out.println("<head ><meta charset='utf-8'/><title>测试</title></head>");
out.println("<body>这是测试servlet,time = "+date+"</body>");
out.println("</html>");
out.flush();
out.close();
}
}
web.xml中配置访问路径映射关系
<serv