溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

springboot如何配置嵌入式servlet容器

發(fā)布時(shí)間:2020-10-28 19:37:19 來(lái)源:億速云 閱讀:170 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

springboot如何配置嵌入式servlet容器?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

配置嵌入式Servlet容器

springboot默認(rèn)tomcat嵌入式servlet容器,所以不用在配置tomcat。

1.如何定制修改servlet容器?

1.在applicatio.properties里修改和server有關(guān)的配置(推薦)
如:

server.tomcat
server.tomcat.connection-timeout=  連接超時(shí)時(shí)間
server.tomcat.uri-encoding=UTF-8 修改編碼

server.servlet.XXX 通用servlet容器設(shè)置
server.tomcatXXX 一般是Tomcat的設(shè)置

2.通過(guò)代碼的方式修改配置

-編寫一個(gè)配置類 @Configuration 注解即可
-在里面添加一個(gè)ConfigurableServletWebServerFactory定制器

@Bean
 public ConfigurableServletWebServerFactory configurableServletWebServerFactory(){
 TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();//這里是一個(gè)工廠
 factory.setPort(8088);//修改端口號(hào)
 //factory.setContextPath("/lanqiao518");
 return factory;
 }

-返回定制器,并注入到容器中 @Bean ,這個(gè)時(shí)候就配置好了

3.注冊(cè)Servlet三大組件(servlet、Listener【監(jiān)聽(tīng)器】,Filter【過(guò)濾器】)

首先要自己寫一個(gè)類,分別實(shí)現(xiàn)三大組件自己的接口然后才能添加進(jìn)容器中

這里以servlet為例:

springboot如何配置嵌入式servlet容器

Servlet:

public class MyServlet extends HttpServlet {//寫好servlet下一步給他添加進(jìn)容器中
 @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.setContentType("text/html;charset=utf-8");
  resp.setCharacterEncoding("utf-8");
  resp.getWriter().write("hello 你好");
 }
}

listener:

public class Mylistener implements ServletContextListener {
 @Override
 public void contextInitialized(ServletContextEvent sce) {

  System.out.println("項(xiàng)目啟動(dòng)了————————————————————————");
 }

 @Override
 public void contextDestroyed(ServletContextEvent sce) {
  System.out.println("項(xiàng)目關(guān)閉了————————————————————————");
 }
}

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攔截了----------------------");
  filterChain.doFilter(servletRequest,servletResponse);//然后放行
 }

 @Override
 public void destroy() {

 }
}

1.將自己寫的servlet注入容器:

@Configuration
public class MyServletConfig {
 //將自己寫的servlet添加進(jìn)容器
@Bean
 public ServletRegistrationBean myServlet(){
  ServletRegistrationBean registrationBean=new ServletRegistrationBean(new MyServlet(),"/my");//將自己寫的servlet添加進(jìn)來(lái),并給一個(gè)訪問(wèn)路徑
  return registrationBean;
 }
}

2.將自己寫的Linstener注入容器

@Bean
public ServletListenerRegistrationBean myListener() {
 ServletListenerRegistrationBean listenerRegistrationBean = new ServletListenerRegistrationBean(new Mylistener());//傳一個(gè)自己寫的監(jiān)聽(tīng)器即可
 return listenerRegistrationBean;
}

3.將自己寫的Filter注入容器

@Bean
public FilterRegistrationBean myFilter(){
 FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(new MyFilter(),myServlet());//傳一個(gè)自己寫的Filter 和你要攔截的servlet 可以是多個(gè)
 return filterRegistrationBean;
}

注意:注入時(shí)的名稱和自己編寫的類名一致

springboot如何配置嵌入式servlet容器

關(guān)于springboot如何配置嵌入式servlet容器問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI