Web开发使用Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。当使用Spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。
Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?
方法一:通过ServletRegistrationBean进行代码注册
Servlet代码:
public class Servlet1 extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
System.out.println("Servlet初始化");
super.init();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-----------> doGet()");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-----------> doPost()");
resp.setContentType("text/html");
String id = req.getParameter("id");
if(id != null && !id.equals("")){
resp.getWriter().append("-----------> doPost() id=" + id);
return;
}
resp.getWriter().append("-----------> doPost()");
}
}
注册代码:
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new Servlet1(),"/servlet/*");// ServletName默认值为首字母小写,即servlet
}
方法二:使用注解注册Servlet
Servlet代码:
/**
* 使用注解注册Servlet
*
*/
//不指定name的情况下,name默认值为类全路径,即org.springboot.sample.servlet.MyServlet2
@WebServlet(urlPatterns="/servlet/servlet2", description="Servlet的说明")
public class Servlet2 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
System.out.println("Servlet初始化");
super.init();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-----------> doGet()");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-----------> doPost()");
resp.setContentType("text/html");
String id = req.getParameter("id");
if(id != null && !id.equals("")){
resp.getWriter().append("-----------> doPost() id=" + id);
return;
}
resp.getWriter().append("-----------> doPost()");
}
}
添加@ServletComponentScan注解:
使用注解方式注册Servlet需要在SpringbootHelloApplication里添加@ServletComponentScan注解
@SpringBootApplication
@ServletComponentScan //使用注解的方式注册servlet需要在SpringbootHelloApplication.java中添加@ServletComponentScan注解
public class SpringbootHelloApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloApplication.class, args);
}
}
注意:可以通过@ServletComponentScan注解里的basePackages属性指定加载指定包下的Servlet。
修改DispatcherServlet默认配置
/**
* 修改DispatcherServlet默认配置
*
* @param dispatcherServlet
*/
@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.getUrlMappings().clear();
registration.addUrlMappings("*.action"); //只有*.action 的请求能通过
registration.addUrlMappings("*.json");
return registration;
}