开发Servlet的步骤:
步骤一:编写一个实现Servelt接口或继承HttpServlet的Java类,并重写service方法
步骤二:获取输出流PrintWriter,response.getWriter();
步骤三:out.print(“<h1<helloServlet</h1>”);
步骤四:out.close();
步骤五:部署到Tomcat
步骤六:启动Tomcat
步骤七:访问地址localhost:8080/项目名称/url-pattern
public class HelloServlet extends HttpServlet{
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("helloServlet");
//out.write("helloServlet");
}
}
注意:
out.print()与out.write()的区别:
唯一的区别就是对null的输出:
out.print()是直接输出null
out.write()是将null转换为字符串输出到页面,也就是输出空字符串。