1.写的servlet类继承HttpServlet
重写doget(),doPost()方法
2.添加注解@WebServlet(urlPatterns="myservlet")
3在启动main方法中@ServletComponentScan("com.lgt.servlet")
方式二
1.写一个普通的servlet类不用加注解
2.写一个spring配置类
如
@Configuration
public class ServletConfig {
@Bean
public ServletRegistrationBean myRegistrationBean(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(),"/servlet/myservlet");
return servletRegistrationBean;
}
}