一、注解方式
1 @WebServlet(urlPatterns = "/myServlet") 2 public class MyServlet extends HttpServlet { 3 4 5 // 访问方式: http://localhost:8080/demo/myServlet 6 7 private static final long serialVersionUID = -7864798058212193346L; 8 9 @Override 10 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 11 response.getWriter().print("My Servlet Hello World!!!"); 12 response.getWriter().flush(); 13 response.getWriter().close(); 14 } 15 16 @Override 17 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 18 this.doGet(request, response); 19 } 20 }
二、配置Bean+@Controller
1 public class HeServlet extends HttpServlet { 2 3 private static final long serialVersionUID = 9040678112053909135L; 4 5 @Override 6 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 7 response.getWriter().print("He Servlet Hello World!!!"); 8 response.getWriter().flush(); 9 response.getWriter().close(); 10 } 11 12 @Override 13 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 14 this.doGet(request, response); 15 } 16 17 }
三、配置Bean+@Configuration
源码地址:https://github.com/liushaoye/05-filter-new