17.配置嵌入式的Servlet容器
阿新 • • 發佈:2022-05-10
springboot使用的是嵌入式的tomcat伺服器
1.如何修改和定製servlet容器的相關配置
springboot是否支援其他的servlet容器修改servlet有關的配置(ServerProperties) 例如在application.properties server.port=8081 server.context-path=/crud server.tomcat.uri-encoding=utf-8 1.通用的servlet容器設定 server.xxx 2.tomcat的設定 server.tomcat.xxx 3.還可以通過嵌入式的servlet容器定製器(WebServerFactoryCustomizer),來修改容器的配置 @Configuration public class MyServerConfig { @Bean public WebServerFactoryCustomizer WebServerFactoryCustomizer(){ return new WebServerFactoryCustomizer() { @Override public void customize(WebServerFactory factory) { ConfigurableWebServerFactory factory1= (ConfigurableWebServerFactory) factory; factory1.setPort(8083); } }; } } -------------------------------------------------------------------------------- 如何註冊servlet的三大元件 由於springbootmore是以jar包的形式啟動嵌入式的servlet容器來啟動springboot的web應用,沒有web.xml檔案,註冊三大元件(servlet,filter,listener)以以下的方式 1.註冊servlet 1.1 建立自己的servlet,處理doget/dopost請求 public class Myservlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().println("hello myservlet"); } } 1.2.配置servlet並載入到容器中 @Configuration public class MyServerConfig { //配置嵌入式的servlet:設定埠為8083 @Bean public WebServerFactoryCustomizer webServerFactoryCustomizer(){ return new WebServerFactoryCustomizer() { @Override public void customize(WebServerFactory factory) { ConfigurableWebServerFactory factory1= (ConfigurableWebServerFactory) factory; factory1.setPort(8083); } }; } //配置servlet的三大元件 //1.註冊servlet @Bean public ServletRegistrationBean myservlet(){ //使用ServletRegistrationBean:引數1(自己的servlet),引數2(自建的servlet處理的請求路徑) ServletRegistrationBean registrationBean = new ServletRegistrationBean(new Myservlet(), "/myservlet"); return registrationBean; } } 1.3訪問路徑:http://localhost:8083/myservlet 頁面出現:hello myservlet 說明自建的servlet起作用了,配置成功! 2.註冊filter 2.1建立自己的filter public class Myfilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("MyFilter process"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } } 2.2將自己的filter註冊到容器中 //註冊filter @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean registrationBean=new FilterRegistrationBean(); registrationBean.setFilter(new Myfilter()); registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet")); return registrationBean; } 2.3.當輸入路徑是:http://localhost:8083/myServlet 後臺列印:MyFilter process 3.註冊listener 3.1自己建立的lister public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContex物件初始化...web應用啟動!"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContex銷燬...伺服器關閉!"); } } 3.2將自己建立的listener載入到容器中 //註冊listener @Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean listenerRegistrationBean=new ServletListenerRegistrationBean(new MyListener()); return listenerRegistrationBean; } 3.3 當程式啟動時:列印:ServletContex物件初始化...web應用啟動! 當程式正常停止時:列印:ServletContex銷燬...伺服器關閉!
1.tomcat(預設使用)
2.Jetty(長連線)
3.Undertow(不支援jsp)