填写包名
填写类名
勾选上init、destory、doGet、doPost方法
如果Servlet报错,则将tomcat安装目录下lib路径下的Servlet-api.jar拷贝到项目的WEB-INF/lib下,
如果报错没有消失,则在该jar包上右键,将其添加进Build Path.
在ABCSsevlet代码上右键,选择Run As, Run On Server
并将要运行的项目移动到右边,点击Finish.
成功运行后,可在Eclipse中或浏览器中查看运行结果
package com.amnotgcs.get.controller; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/ABCServlet") public class ABCServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ABCServlet() { super(); System.out.println("执行ABCServlet的构造方法"); } public void init(ServletConfig config) throws ServletException { System.out.println("执行ABCServlet的init方法"); } public void destroy() { System.out.println("执行ABCServlet的destory方法"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("执行ABCServlet的doGet方法"); response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("执行ABCServlet的doPost方法"); doGet(request, response); } }
再次运行项目可在控制台观察到如下结果(停止Server后可观察到destory方法的执行)