1、pom.xml导入包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
2、继承HttpServlet类,重写需要的方法
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3、在web.xml中申明
<servlet>
<!--自定义名称-->
<servlet-name>HelloServlet</servlet-name>
<!--关联类-->
<servlet-class>com.doubleh.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--映射对应的servlet-->
<servlet-name>HelloServlet</servlet-name>
<!--路由地址-->
<url-pattern>/hello</url-pattern>
</servlet-mapping>