1、必须继承HttpServlet类
package com.azhi.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost"); } }
2、必须在web.xml上注册自定义的servlet
<!-- 注册servlet --> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>com.azhi.servlet.LoginServlet</servlet-class> </servlet> <!-- 映射servlet --> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>