1. Introduce maven dependency packages
1.1 Introduce javax.servlet-api package in pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
2. Implement inheritance of httpserlet class
2.1 Custom method of implementing httpserlet
public class TestServletController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get请求");
resp.getWriter().write("请求1");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
3. webapp/WEB-INF/web.xml configuration
3.1 Configure servlet
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>com.test.TestServletController</servlet-class>
</servlet>
The servlet-name name can be customized with the class annotation in which
servlet-class has taken effect.Multiple servlet tags can be used to configure multiple servlet implementation classes to achieve corresponding logical control according to different situations.
3.1 Configure servlet-mapping
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
servlet-name must be consistent with the name set by servlet-name, identifying which configuration class takes effect.The url matching path defined by
url-pattern