溫馨提示×

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

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

SpringBoot整合三大組建(Servlet、Liste

發(fā)布時(shí)間:2020-02-25 01:48:19 來(lái)源:網(wǎng)絡(luò) 閱讀:198 作者:liduchang 欄目:軟件技術(shù)

更多資源和教程請(qǐng)關(guān)注公眾號(hào):非科班的科班。
如果覺(jué)得我寫(xiě)的還可以請(qǐng)給個(gè)贊,謝謝大家,你的鼓勵(lì)是我創(chuàng)作的動(dòng)力

3.SpringBoot整合Servlet

3.1.方式一

步驟:

  • 寫(xiě)一個(gè)類MyFirstServlet繼承HttpServlet,并重寫(xiě)doGet方法。
  • 在類的上面用@WebServlet標(biāo)識(shí)Servlet并指明name和urlPatterns。
  • 在標(biāo)識(shí)有@SpringBootApplication的主類上加上@ServletComponentScan。

FirstServlet.java

package com.example.servlet.myservlet;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *SpringBoot整合Servlet方式一
 *@WebServlet(name="MyFirstServlet",urlPatterns="/myFirst")相當(dāng)于如下:
 *
 *<servlet>
 * <servlet-name>MyFirstServlet</servlet-name>
 * <servlet-class>ah.szxy.servlet.FirstServlet</servlet-class>
 *</servlet>
 *<servlet-mapping>
 * <servlet-name>MyFirstServlet</servlet-name>
 * <url-pattern>/first</url-pattern>
 *</servlet-mapping>
 *
 */

@WebServlet(name="MyFirstServlet",urlPatterns="/myFirst")
public class FirstServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyFirstServlet init............");
    }
}

ServletApplication.java

package com.example.servlet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan //在springBoot啟動(dòng)時(shí)會(huì)掃描@WebServlet,并將該類實(shí)例化
public class ServletApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServletApplication.class, args);
   }

}

然后啟動(dòng)項(xiàng)目
SpringBoot整合三大組建(Servlet、Liste

最后在瀏覽器輸入localhost:8080/myFirstServlet,頁(yè)面顯示空白,在控制臺(tái)打印MyFirstServlet init............

3.2.方式二

步驟:

  • 創(chuàng)建一個(gè)類SecondServlet繼承HttpServlet,并重寫(xiě)doGet方法。
  • 在@SpringBootApplication標(biāo)識(shí)的主類中加@Bean的一個(gè)方法。
    SecondServlet.java
package com.example.servlet.myservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 整合Servlet的第二種方式
 */
public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MySecondServlet init..........");
    }
}

ServletApplication.java

package com.example.servlet;

import com.example.servlet.myservlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
//@ServletComponentScan //在springBoot啟動(dòng)時(shí)會(huì)掃描@WebServlet,并將該類實(shí)例化
public class ServletApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServletApplication.class, args);
   }

  /**
 * 整合Servlet的第二種方式,創(chuàng)建ServletRegistrationBean并添加路徑
 * @return
 */
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
   ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
   bean.addUrlMappings("/mySecond");
   return bean;
}

然后啟動(dòng)項(xiàng)目,在瀏覽器中訪問(wèn)localhost:8080/mySecondServlet,頁(yè)面也是空白,在控制臺(tái)就會(huì)打印MySecondServlet init..........
SpringBoot整合三大組建(Servlet、Liste
項(xiàng)目,結(jié)構(gòu)如圖所示
SpringBoot整合三大組建(Servlet、Liste

結(jié)論:

  • 上面的兩種方式推薦使用第一種基于注解的整合。
  • 雖然現(xiàn)在幾乎用不到servlet了,但是學(xué)習(xí)SpringBoot整合servlet有助于學(xué)習(xí)的深入了解,更好的理解框架。

4.SpringBoot整合Filter

4.1.方式一

步驟:

  • 創(chuàng)建一個(gè)MyFirstFilter類實(shí)現(xiàn)Filter接口,并在類上面標(biāo)注@WebFilter。
  • 在@SpringBootApplication的主類上加上@ServletComponentScan注解。

MyFirstFilter.java

package com.example.servlet.myfilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 基于@WebFilter注解整合Filter方式一
 */
@WebFilter(filterName = "MyFirstFilter",urlPatterns = "/myFirst")
public class MyFirstFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
        System.out.println("進(jìn)入Filter中了.....");
        arg2.doFilter(arg0,arg1);
        System.out.println("離開(kāi)Filter了.......");
    }

    @Override
    public void destroy() {

    }
}

ServletApplication.java

package com.example.servlet;

import com.example.servlet.myservlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@ServletComponentScan //在springBoot啟動(dòng)時(shí)會(huì)掃描@WebServlet,并將該類實(shí)例化
public class ServletApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServletApplication.class, args);
   }

   /**
    * 整合Servlet的第二種方式,創(chuàng)建ServletRegistrationBean并添加路徑
    * @return
    */
   @Bean
   public ServletRegistrationBean getServletRegistrationBean(){
      ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
      bean.addUrlMappings("/mySecond");
      return bean;
   }
}

4.2.方式二

步驟:

  • 創(chuàng)建一個(gè)類MySecondFilter實(shí)現(xiàn)Filter接口,重寫(xiě)方法。
  • 在@SpringBootApplication標(biāo)識(shí)的主類中加@Bean的一個(gè)方法,將MySecondFilter對(duì)象注入容器中。

MySecondFilter.java

package com.example.servlet.myfilter;

import javax.servlet.*;
import java.io.IOException;

/**
 * 整合Filter的第二種方式
 */
public class MySecondFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
        System.out.println("進(jìn)入MySecondFilter了......");
        arg2.doFilter(arg0, arg1);
        System.out.println("離開(kāi)MySecondFilter了......");
    }

    @Override
    public void destroy() {

    }
}

ServletApplication.java

package com.example.servlet;

import com.example.servlet.myfilter.MySecondFilter;
import com.example.servlet.myservlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
//@ServletComponentScan //在springBoot啟動(dòng)時(shí)會(huì)掃描@WebServlet,并將該類實(shí)例化
public class ServletApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServletApplication.class, args);
   }

   /**
    * 整合Filter的第二種方式
    * 注冊(cè)Filter
    */
   @Bean
   public FilterRegistrationBean getFilterRegistrationBean() {
      FilterRegistrationBean bean = new FilterRegistrationBean(new MySecondFilter());
      // bean.addUrlPatterns(new String[]{"*.do","*.jsp"});//攔截多個(gè)時(shí)
      bean.addUrlPatterns("/mySecond");
      return bean;
   }
}

然后在瀏覽器訪問(wèn)localhost:8080/mySecond,就可以看到控制臺(tái)打印如下
SpringBoot整合三大組建(Servlet、Liste

5.SpringBoot整合Listener

5.1.方式一

步驟:

  • 創(chuàng)建一個(gè)類MyFirstListener實(shí)現(xiàn)ServletContextListener接口,重寫(xiě)方法
  • 在該類上加上@WebListener注解
package com.example.servlet.mylistener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * springBoot 整合Listener第一種方式
 * 創(chuàng)建一個(gè)Servlet上下文的監(jiān)聽(tīng)器
 * @WebListener 自動(dòng)注冊(cè),相當(dāng)于在web.xml中添加如下代碼
 *
 *<listener>
 * <listener-class>ah.szxy.listener.FirstListener</listener-class>
 *</listener>
 */
@WebListener
public class MyFirstListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("MyFirstListener執(zhí)行銷毀了。。。");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("MyFirstListener執(zhí)行初始化了。。。");
    }
}

執(zhí)行項(xiàng)目會(huì)打印如下,因?yàn)橛昧薂ServletComponentScan注解,在項(xiàng)目啟動(dòng)的時(shí)候就會(huì)掃描包中是否含有servlet,若有就初始化。由于FirstServlet是基于注解初始化的,所以在項(xiàng)目啟動(dòng)的時(shí)候,就會(huì)執(zhí)行初始化servlet,被Listener監(jiān)聽(tīng)到
SpringBoot整合三大組建(Servlet、Liste

5.1.方式二

步驟:

  • 創(chuàng)建一個(gè)類MySecondListener實(shí)現(xiàn)ServletContextListener接口,重寫(xiě)方法。
  • 在@SpringBootApplication標(biāo)識(shí)的主類中加@Bean的一個(gè)方法,將MySecondListener對(duì)象注入容器中。
package com.example.servlet.mylistener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * 整合Listener的第二種方式
 */
public class MySecondListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("MySecondListener執(zhí)行銷毀了。。。");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("MySecondListener執(zhí)行初始化了。。。");
    }

}
package com.example.servlet;

import com.example.servlet.myfilter.MySecondFilter;
import com.example.servlet.mylistener.MySecondListener;
import com.example.servlet.myservlet.SecondServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@ServletComponentScan //在springBoot啟動(dòng)時(shí)會(huì)掃描@WebServlet,并將該類實(shí)例化
public class ServletApplication {

   public static void main(String[] args) {
      SpringApplication.run(ServletApplication.class, args);
   }

   /**
    * 注冊(cè)listener
    */
   @Bean
   public ServletListenerRegistrationBean<MySecondListener> getServletListenerRegistrationBean() {
      ServletListenerRegistrationBean<MySecondListener> bean = new ServletListenerRegistrationBean<MySecondListener>(
            new MySecondListener());
      return bean;
   }

}

執(zhí)行項(xiàng)目,在控制臺(tái)可以看到輸出如下,兩個(gè)Servlet監(jiān)聽(tīng)器都執(zhí)行了
SpringBoot整合三大組建(Servlet、Liste
總的項(xiàng)目目錄包結(jié)構(gòu)如下:
SpringBoot整合三大組建(Servlet、Liste

更多資源和教程請(qǐng)關(guān)注公眾號(hào):非科班的科班
如果覺(jué)得我寫(xiě)的還可以請(qǐng)給個(gè)贊,謝謝大家,你的鼓勵(lì)是我創(chuàng)作的動(dòng)力

最后分享一波java的資源,資源包括java從入門到開(kāi)發(fā)的全套視頻,以及java的26個(gè)項(xiàng)目,資源比較大,大小大概是290g左右,鏈接容易失效,獲取的方式是關(guān)注公眾號(hào):非科班的科班,讓后回復(fù):java項(xiàng)目即可獲得,祝大家學(xué)習(xí)愉快

向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