1. 程式人生 > >【JavaWeb】IDEA下 Tomcat下執行servlet

【JavaWeb】IDEA下 Tomcat下執行servlet

新建Module

建成後的目錄:

新建Servlet類

public class AServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init");
    }
​
    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig");
        return null;
    }
​
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service");
    }
​
    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo");
        return null;
    }
​
    @Override
    public void destroy() {
        System.out.println("destroy");
​
    }
}

配置web.xml

 <?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>xxx</servlet-name>
        <servlet-class>com.haya.web.AServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>xxx</servlet-name>
        <url-pattern>/A</url-pattern>
    </servlet-mapping>
</web-app>

最後