用IDEA创建Servlet
选择Use module compile output path,将Output path和Test output path都选择为刚才创建的classes路径下,然后点击下方的Apply
同时配置Tomcat,Run -> Edit Configuration -> 点击左上角“+”号 -> “Tomcat Server” -> “Local”
在"Name"处输入服务名,点击"Application server"后面的"Configure…",弹出Tomcat Server窗口,选择本地安装的Tomcat目录,点击 Apply -> OK
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author ZhouYanXiang */ public class Servlet1 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { System.out.println("----调用doGet()方法----"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { System.out.println("----调用doPost()方法----"); //调用doGet方法 doGet(request,response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>Servlet1</servlet-name> <servlet-class>Servlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <url-pattern>/Servlet1</url-pattern> </servlet-mapping> </web-app>