内置Servlet使用
1、如何定利和修改Servlet容器的相关配置?
1.1 修改server有关的配置(ServerProperties)
通用的servlet容器设置
server.xx
tomcat 设置
server.tomcat.xx
1.1 或者编写一个 WebServerFactoryCustomizer:嵌入式servlet容器的定制器
@Component public class MyTomcatWebServerCustomizer implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> { @Override public void customize(TomcatServletWebServerFactory factory) { // customize the factory here } }
1.2 注册Servlet、Filter、Listener三大组件
对于标注的web目录结构,我们可以在web.xml中配置这三大组件
使用嵌入式servlet注册三大组件
1、注册Servlet
class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("xxx"); } } @Configuration public class MyServletConfig { //注册servlet @Bean public ServletRegistrationBean servletRegistrationBean(){ ServletRegistrationBean<MyServlet> myServletServletRegistrationBean = new ServletRegistrationBean<>(new MyServlet(), "/xx"); return myServletServletRegistrationBean; } }
2、注册Filter
class Myfilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("dofilter"); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { } } @Configuration public class MyFilterConfig { @Bean public FilterRegistrationBean filterRegistrationBean(){ //可以传入之前注册的ServletRegistrationBean //FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(new Myfilter(), new ServletRegistrationBean()); //指定的url FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>(); filterFilterRegistrationBean.setFilter(new Myfilter()); filterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/xx")); return filterFilterRegistrationBean; } }
3、注册listener
class MyListener implements ServletRequestListener, ServletRequestAttributeListener, HttpSessionListener, HttpSessionAttributeListener, ServletContextListener, ServletContextAttributeListener { @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("初始化request"); } } @Configuration public class MyListenerConfig { @Bean public ServletListenerRegistrationBean servletListenerRegistrationBean(){ ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(); servletListenerRegistrationBean.setListener(new MyListener()); return servletListenerRegistrationBean; } }
1.3配置其他嵌入式Servlet容器
1.3.1 springboot支持的嵌入式servlet容器
jetty:适合做聊天,保持长连接
Undertow
netty
tomcat
1.3.2 切换成jetty
1、排除默认依赖
2、导入jetty
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
3、直接启动
2、使用外置的Servlet容器
2.1嵌入式Servlet容器:
jar优点:简单、便携;
缺点:默认不支持ISP、优化定制比较复杂(使用定利器【ServerProperties、自定义EmbeddedServletContainerCustomizer】,自己编写嵌入式Servlet容器的创建工厂
【EmbeddedServletContainerFactory】);
2.2 外置Servlet容器:外面安装tomcat---应用war包方式打包
2.2.1 创建springboot项目
2.2.2 生成的目录
、
2.2.3 配置tomcat服务器
2.2.4 测试1
2.2.4.1 application.properties
spring.mvc.view.prefix=/WEB-INF/views/ spring.mvc.view.suffix=.jsp
2.2.4.2 创建/WEB-INF/views/hellow.jsp
2.2.4.3 controller,访问localhost:8080/test
@Controller public class UserController { @GetMapping("/test") public String test(){ //如果没有aplication.properties中的配置 //return "/WEB-INF/views/hellow.jsp"; return "hellow"; } }
2.2.4 测试2
导入 thymeleaf,controller中 return "hellow"; 返回的是templates下的html资源了。所以也就访问不到 WEB-INF/views/hellow.jsp,只有将thymeleaf删除,才可以访问/WEB-INF/views/xx.jsp
2.2.4 测试3
2.2.4.1 在static目录下创建hellow.html
2.2.4.2 可以访问 localhost:8080/hellow.html
3、嵌入式Servlet和外置Servlet启动原理
jar包:可执行的jar包可以启动主程序(springBoot main方法),启动IOC容器,创建嵌入式Servlet容器。
war包:启动服务器,服务器启动springBoot应用(SpringBootServletInitializer),启动IOC容器。