嵌入式Servlet容器注册三大组件
嵌入式Servlet容器注册三大组件
1、Servlet组件
(1)写自己的MyServlet.java类
1 public class MyServlet extends HttpServlet { 2 //处理get请求 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 doPost(req, resp); 6 } 7 //处理post请求 8 @Override 9 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 10 resp.getWriter().write("Hello MyServlet!"); 11 } 12 }
(2)将自己写的组件添加进容器中
1 @Configuration 2 public class MyServerConfig { 3 4 //注册三大组件 5 @Bean 6 public ServletRegistrationBean myServlet(){ 7 ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/servlet"); 8 return registrationBean; 9 } 10 }
2、Filter组件
(1)写自己的 MyFilter.java 类
注意:导入的是import javax.servlet.*; 这个类
1 import javax.servlet.*; 2 import java.io.IOException; 3 4 public class MyFilter implements Filter { 5 @Override 6 public void init(FilterConfig filterConfig) throws ServletException { 7 8 } 9 10 @Override 11 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 12 System.out.println("MyFilter!!!!"); 13 chain.doFilter(request,response); 14 } 15 16 @Override 17 public void destroy() { 18 19 } 20 }
(2)把自己写的组件添加到容器中
1 //Filter 2 @Bean 3 public FilterRegistrationBean myFilter(){ 4 FilterRegistrationBean registrationBean = new FilterRegistrationBean(); 5 registrationBean.setFilter(new MyFilter()); 6 //拦截 7 registrationBean.setUrlPatterns(Arrays.asList("/hello","/servlet")); 8 return registrationBean; 9 }
3、Listener组件
(1)写自己的MyListener.java类
1 public class MyListener implements ServletContextListener { 2 @Override 3 public void contextInitialized(ServletContextEvent sce) { 4 System.out.println("contextInitialized web 启动"); 5 } 6 7 @Override 8 public void contextDestroyed(ServletContextEvent sce) { 9 System.out.println("contextInitialized 当前web项目销毁"); 10 } 11 }
(2)吧自己写的组件添加到容器中
1 //Listener 2 @Bean 3 public ServletListenerRegistrationBean myListener(){ 4 ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean(new MyListener()); 5 return listenerRegistrationBean; 6 }
4、切换为其他的嵌入式Servlet容器
默认支持:Tomcat(默认)、Jetty、Undertow(不支持JSP)
修改项目的pom.xml文件就可以切换容器
(1)默认使用Tomcat
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 </dependency>
(2)切换为Jetty
1 <!--排除了Tomcat--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <exclusions> 6 <exclusion> 7 <artifactId>spring-boot-starter-tomcat</artifactId> 8 <groupId>org.springframework.boot</groupId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 <!--引入其他的Servlet容器:J--> 13 <dependency> 14 <artifactId>spring-boot-starter-jetty</artifactId> 15 <groupId>org.springframework.boot</groupId> 16 </dependency>
(3)切换为UnderTow
1 <!--排除了Tomcat--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 <exclusions> 6 <exclusion> 7 <artifactId>spring-boot-starter-tomcat</artifactId> 8 <groupId>org.springframework.boot</groupId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 <!--引入其他的Servlet容器:J--> 13 <dependency> 14 <artifactId>spring-boot-starter-undertow</artifactId> 15 <groupId>org.springframework.boot</groupId> 16 </dependency>
5、使用外置的Servlet容器
嵌入式Servlet容器:jar
- 优点:简单、便携;
- 缺点:默认不支持JSP,优化定制比较复杂,自己编写嵌入式Servlet容器的创建工厂‘’
外置的Servlet容器:外面安装Tomcat--应用war包的方式打包;
(1)创建项目

(2)创建webapp文件夹
第一步:Setting -> Project Structure...

第二步:Modules -> Web
双击Web Resource Directory,在弹出窗口点击确认,创建webapp文件夹

(3)创建web.xml配置文件


应用配置之后就可以在目录下看到web.xml文件了

(4)配置Tomcat
选择Edit Configuraions

点击+号

添加Tomcat本地的服务器


修改设置

将Tomcat与部署项目关联

在弹出窗口选择demo:war exploded,
最后点击确定应用配置

选择Tomcat运行项目

(5)将嵌入式的Tomcat指定为provided
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-tomcat</artifactId> 4 <scope>provided</scope> 5 </dependency>
(6)必须编写一个 SpringBootServletInitializer 的子类,并调用 configure 方法
1 public class ServletInitializer extends SpringBootServletInitializer { 2 3 @Override 4 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 5 //传入SpringBoot应用的主程序 6 return application.sources(DemoApplication.class); 7 } 8 }
(7)完成!

浙公网安备 33010602011771号