<!--引入父項目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <dependencies> <!--SpringBoot web啓動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
1.2. 編寫Servlet代碼:java
@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns:訪問路徑 public class firstServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("進來了firstServlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }
1.3. 編寫啓動類
建立springboot啓動類
代碼:web
@SpringBootApplication //在spring boot啓動時會掃描@WebServlet註解,並建立該類的實例 @ServletComponentScan public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
注意:在啓動類上須要加上@ServletComponentScan註解 意思是:在啓動時掃描@WebServlet註解 ,建立Servlet的實例spring
1.4. 運行啓動類 ,在瀏覽器輸入localhost:8080/firstServlet
控制檯輸出信息
瀏覽器
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } //添加一個方法,方法名無要求,必須返回ServletRegistrationBean。註冊Servlet對象 @Bean //主鍵等價於<bean>標籤 public ServletRegistrationBean<SecondServlet> getServletRegistrationBean(){ ServletRegistrationBean<SecondServlet> bean= new ServletRegistrationBean<SecondServlet>(new SecondServlet(),"/SecondServlet"); return bean; } }
@WebFilter(filterName = "firstFilter", urlPatterns = "/firstFilter") public class firstFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("----進入FirstFilter-----"); chain.doFilter(request, response);//放行 System.out.println("----離開FirstFilter-----"); } }
@SpringBootApplication //在spring boot啓動時會掃描@WebServlet @WebFilter @WebListener註解,並建立該類的實例 @ServletComponentScan public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
運行啓動類,在瀏覽器輸入 localhost:8080/firstFilter
這裏報404是由於沒有寫放行後的路徑;springboot
控制檯打印信息:
app
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } //添加一個方法 @Bean public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean(){ FilterRegistrationBean<SecondFilter> bean= new FilterRegistrationBean<SecondFilter>(new SecondFilter()); bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //以.do , .jsp , SecondFilter結尾路徑的都會進到過濾器 return bean; } }
@WebListener() public class firstListener implements ServletContextListener{ //監聽application對象的建立 @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("-----------application對象建立-----------------"); } }
@SpringBootApplication @ServletComponentScan //在spring boot啓動時會掃描@WebServlet @WebFilter @WebListener註解,並建立該類的實例 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
代碼同樣 省略代碼 直接上代碼webapp
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } @Bean public ServletListenerRegistrationBean<firstListener> getServletListenerRegistrationBean(){ ServletListenerRegistrationBean<firstListener> bean= new ServletListenerRegistrationBean<firstListener>(new firstListener()); return bean; } }
1.在src/main 下建立一個webapp的目錄(目錄名稱必須爲webapp)
在webapp下建立不一樣目錄存放不一樣的靜態資源,如:images 放圖片 .
2. 運行啓動類訪問 直接訪問資源路徑
jsp
在src/main/resources下建立一個static的目錄(目錄名稱必須爲static)
在static下建立不一樣目錄存放不一樣的靜態資源,如:images 放圖片 .
2. 運行啓動類訪問瀏覽器 直接訪問資源路徑
maven
以上就是本教程的相關內容,感謝觀看,轉載請註明出處ide