013-Spring Boot web【二】静态资源、Servlet、Filter、listenter

一、静态资源

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");
    }
}
View Code

并且在启动类上增加Servlet扫描注解:ServletComponentScan

@ServletComponentScan
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        //context.close();
    }
}
View Code

访问即可

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

    }

}
View Code

此时运行即可

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());

    }

}
View Code

此时运行即可

三、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");
    }
}
View Code

增加配置类,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;
    }

}
View Code

此时访问即可。

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

    }

}
View Code

在配置类,ServletConfiguration.java增加

    @Bean
    public FilterRegistrationBean createFilterRegistrationBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new EchoFilter());
        filter.setUrlPatterns(Arrays.asList("/book.do"));
        return filter;
    }
View Code

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("=================");
    }

}
View Code

在配置类,ServletConfiguration.java增加

    @Bean
    public ServletListenerRegistrationBean<StartupListener> createServletListenerRegistrationBean() {
        ServletListenerRegistrationBean<StartupListener> registrationBean = new ServletListenerRegistrationBean<StartupListener>(new StartupListener());
        return registrationBean;
    }
View Code

注:一般使用2中注解,因为Spring boot基于spring 4.x ,spring 4.x 开发的web一般使用Servlet3.x以上

 

posted @ 2018-02-06 22:18  bjlhx15  阅读(383)  评论(0编辑  收藏  举报
Copyright ©2011~2020 JD-李宏旭