问:有了springMVC,为什么还要用servlet?有了servlet3的注解,为什么还要使用ServletRegistrationBean注入的方式?
使用场景:在有些场景下,比如我们要使用hystrix-dashboard,这时候就需要注入HystrixMetricsStreamServlet(第三方的servlet),该servlet是hystrix的组件。
一、代码
1、TestServlet(第一个servlet)
1 package com.xxx.secondboot.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestServlet extends HttpServlet { 11 12 private static final long serialVersionUID = -4619665430596950563L; 13 14 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 System.out.println("zhaojigang servlet"); 17 } 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 this.doGet(req, resp); 22 } 23 }
2、Testservlet2(第二个servlet)
1 package com.xxx.secondboot.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class TestServlet2 extends HttpServlet { 11 12 private static final long serialVersionUID = 3788279972938793265L; 13 14 @Override 15 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 16 System.out.println("zhaojigang servlet2"); 17 } 18 19 @Override 20 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 21 this.doGet(req, resp); 22 } 23 }
3、ServletConfig(servlet注入配置类)
1 package com.xxx.secondboot.servlet; 2 3 import org.springframework.boot.context.embedded.ServletRegistrationBean; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 7 @Configuration 8 public class ServletConfig { 9 10 @Bean 11 public TestServlet testServlet(){ 12 return new TestServlet(); 13 } 14 15 @Bean 16 public ServletRegistrationBean testServletRegistrationBean(TestServlet testServlet){ 17 ServletRegistrationBean registration = new ServletRegistrationBean(testServlet); 18 registration.setEnabled(true); 19 registration.addUrlMappings("/servlet/test"); 20 return registration; 21 } 22 /********************************************/ 23 @Bean 24 public TestServlet2 testServlet2(){ 25 return new TestServlet2(); 26 } 27 28 @Bean 29 public ServletRegistrationBean test2ServletRegistrationBean(TestServlet2 testServlet2){ 30 ServletRegistrationBean registration = new ServletRegistrationBean(testServlet2); 31 registration.setEnabled(true); 32 registration.addUrlMappings("/servlet/test2"); 33 return registration; 34 } 35 36 }
说明:使用ServletRegistrationBean来注入servlet,对于每一个servlet都有一个ServletRegistrationBean来注入。
注意:如果只是自己要使用servlet,可以直接只用servlet3的注解来声明servlet就好,但是像HystrixMetricsStreamServlet这样的第三方servlet,就只能通过上边这样的方式来搞了。
二、测试
启动服务,浏览器输入"http://localhost:8083/servlet/test","http://localhost:8083/servlet/test2",查看console的输出。