您好,登錄后才能下訂單哦!
更多資源和教程請關注公眾號:非科班的科班。
如果覺得我寫的還可以請給個贊,謝謝大家,你的鼓勵是我創作的動力3.SpringBoot整合Servlet
步驟:
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")相當于如下:
*
*<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啟動時會掃描@WebServlet,并將該類實例化
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
然后啟動項目
最后在瀏覽器輸入localhost:8080/myFirstServlet,頁面顯示空白,在控制臺打印MyFirstServlet init............
步驟:
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啟動時會掃描@WebServlet,并將該類實例化
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
/**
* 整合Servlet的第二種方式,創建ServletRegistrationBean并添加路徑
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/mySecond");
return bean;
}
然后啟動項目,在瀏覽器中訪問localhost:8080/mySecondServlet,頁面也是空白,在控制臺就會打印MySecondServlet init..........
項目,結構如圖所示
結論:
步驟:
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("進入Filter中了.....");
arg2.doFilter(arg0,arg1);
System.out.println("離開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啟動時會掃描@WebServlet,并將該類實例化
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
/**
* 整合Servlet的第二種方式,創建ServletRegistrationBean并添加路徑
* @return
*/
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/mySecond");
return bean;
}
}
步驟:
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("進入MySecondFilter了......");
arg2.doFilter(arg0, arg1);
System.out.println("離開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啟動時會掃描@WebServlet,并將該類實例化
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
/**
* 整合Filter的第二種方式
* 注冊Filter
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean() {
FilterRegistrationBean bean = new FilterRegistrationBean(new MySecondFilter());
// bean.addUrlPatterns(new String[]{"*.do","*.jsp"});//攔截多個時
bean.addUrlPatterns("/mySecond");
return bean;
}
}
然后在瀏覽器訪問localhost:8080/mySecond,就可以看到控制臺打印如下
步驟:
package com.example.servlet.mylistener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* springBoot 整合Listener第一種方式
* 創建一個Servlet上下文的監聽器
* @WebListener 自動注冊,相當于在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執行銷毀了。。。");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("MyFirstListener執行初始化了。。。");
}
}
執行項目會打印如下,因為用了@ServletComponentScan注解,在項目啟動的時候就會掃描包中是否含有servlet,若有就初始化。由于FirstServlet是基于注解初始化的,所以在項目啟動的時候,就會執行初始化servlet,被Listener監聽到
步驟:
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執行銷毀了。。。");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub
System.out.println("MySecondListener執行初始化了。。。");
}
}
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啟動時會掃描@WebServlet,并將該類實例化
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
/**
* 注冊listener
*/
@Bean
public ServletListenerRegistrationBean<MySecondListener> getServletListenerRegistrationBean() {
ServletListenerRegistrationBean<MySecondListener> bean = new ServletListenerRegistrationBean<MySecondListener>(
new MySecondListener());
return bean;
}
}
執行項目,在控制臺可以看到輸出如下,兩個Servlet監聽器都執行了
總的項目目錄包結構如下:
更多資源和教程請關注公眾號:非科班的科班。
如果覺得我寫的還可以請給個贊,謝謝大家,你的鼓勵是我創作的動力
最后分享一波java的資源,資源包括java從入門到開發的全套視頻,以及java的26個項目,資源比較大,大小大概是290g左右,鏈接容易失效,獲取的方式是關注公眾號:非科班的科班,讓后回復:java項目即可獲得,祝大家學習愉快
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。