一、静态资源
1.1、webapp默认支持静态资源
在src/main/webapp下建立user.html默认支持访问
1.2、默认内置静态资源目录。可被直接访问
查看包:spring-boot-autoconfigure-1.5.9.RELEASE.jar下的:org.springframework.boot.autoconfigure.web;
查看:ResourceProperties,其中
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
默认放在以上四个目录均可被访问。
可以通过修改配置项,在src/main/resources下的application.properties中
spring.resources.staticLocations=classpath:/html/
二、spring boot中使用Servlet【原始】以来Servlet3.x注解功能
2.1、使用Servlet
新建一个UserServlet.java
package com.lhx.spring.springboot_web_ext; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/user.do") public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("user servlet"); } }
并且在启动类上增加Servlet扫描注解:ServletComponentScan
@ServletComponentScan @SpringBootApplication public class App { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(App.class, args); //context.close(); } }
访问即可
2.2、使用Filter
新建一个LogFilter.java拦截User.do
package com.lhx.spring.springboot_web_ext; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/user.do") public class LogFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("income log filter " + arg0.getRemoteHost()); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
此时运行即可
2.3、使用Listener
package com.lhx.spring.springboot_web_ext; import java.time.LocalDate; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; @WebListener public class MyContextListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("app startup at " + LocalDate.now().toString()); } }
此时运行即可
三、spring boot中使用Servlet【Configuration配置】不依赖Servlet3.0,使用Servlet2.x可以使用
3.1、使用Servlet
增加BookServlet.java,注意没有注解
package com.lhx.spring.springboot_web_ext.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class BookServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("test"); } }
增加配置类,ServletConfiguration.java
package com.lhx.spring.springboot_web_ext.servlet; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; @SpringBootConfiguration public class ServletCongfiguration { @Bean public ServletRegistrationBean createBookServlet() { ServletRegistrationBean servlet = new ServletRegistrationBean(new BookServlet(),"book.do"); return servlet; } }
此时访问即可。
3.2、使用Filter
增加EchoFilter.java
package com.lhx.spring.springboot_web_ext.servlet; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; public class EchoFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) arg0; System.out.println("test" + req.getRequestURI()); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
在配置类,ServletConfiguration.java增加
@Bean public FilterRegistrationBean createFilterRegistrationBean() { FilterRegistrationBean filter = new FilterRegistrationBean(); filter.setFilter(new EchoFilter()); filter.setUrlPatterns(Arrays.asList("/book.do")); return filter; }
3.3、使用Listner
增加StartupListener .java
package com.lhx.spring.springboot_web_ext.servlet; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class StartupListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("================="); } }
在配置类,ServletConfiguration.java增加
@Bean public ServletListenerRegistrationBean<StartupListener> createServletListenerRegistrationBean() { ServletListenerRegistrationBean<StartupListener> registrationBean = new ServletListenerRegistrationBean<StartupListener>(new StartupListener()); return registrationBean; }
注:一般使用2中注解,因为Spring boot基于spring 4.x ,spring 4.x 开发的web一般使用Servlet3.x以上
原文地址:https://www.cnblogs.com/bjlhx/p/8407097.html
时间: 2024-10-08 23:33:52