目录
2.2.2 在启动类上添加注解@ServletComponentScan,并注入到容器中
2.2 Filter,一般urlPatterns = "/*"
1、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、SpringBoot中Servlet的使用
2.1 Servlet
2.1.1 自定义一个servlet,代码如下:
// 这个注解取代了web.xml里的servletName
@WebServlet(name = "myServlet", urlPatterns = "/my")
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
}
}
2.2.2 在启动类上添加注解@ServletComponentScan,并注入到容器中
@SpringBootApplication
@ServletComponentScan
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
@Bean // 注册servlet到spring容器,这里如果指定了servletMapping,会覆盖HelloWorldServlet中的/my
public ServletRegistrationBean<HelloWorldServlet> ServletRegistrationBean(){
ServletRegistrationBean<HelloWorldServlet> bean = new ServletRegistrationBean<HelloWorldServlet>(new HelloWorldServlet(), "/helleServlet");
bean.setLoadOnStartup(1);// 设置加载顺序
return bean;
}
}
2.2 Filter,一般urlPatterns = "/*"
@WebFilter(filterName = "myFilter", urlPatterns = "/filter")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("myFilter -------------init");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("myFilter -------------doFilter");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
System.out.println("myFilter -------------destroy");
}
}
2.3 Listener
listener是Servlet规范定义的一种特殊类,用于监听servletContext,httpSession和ServletRequest等域对象的创建和销毁时间。监听域对象的属性发生变化时的时间,用于在事件的发生前后做一些必要的处理,比如:在线人数统计,系统启动初始化信息,统计网站访问量等
public class MyListener implements HttpSessionListener {
public static long onLine = 0;
@Override
public void sessionCreated(HttpSessionEvent se) {
// session创建的时候执行
onLine++;
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// session销毁的时候执行
onLine--;
}
}
启动类里要将这个listener对象托管给Spring管理
@Bean
public ServletListenerRegistrationBean<MyListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new MyListener());
return bean;
}
3、静态资源的配置
WebMvcAutoConfiguration ResourceProperties自动装配静态资源的核心类。
默认情况下,SpringBoot将在classpath或者ServletContext根目录下从名为/static(/public /resources OR /META-INF/resources)目录中寻找静态资源,它使用了SpringMVC的ResourceHttpRequestHandler,因此,如果我们想自定义静态资源的目录,可以添加自定义的WebMvcConfigurationAdapter,重写addResourceHandlers方法来修改.
首页的配置:SpringBoot支持静态和模板化的欢迎页面。它首先在配置静态资源的位置找index.html文件,如果找不到,查找index模板,如果找到其中任何一个,会自动渲染。