码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-02-07 00:45:33      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:override   xtend   writer   open   静态资源   int   post   configure   request   

一、静态资源

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以上

 

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

标签:override   xtend   writer   open   静态资源   int   post   configure   request   

原文地址:https://www.cnblogs.com/bjlhx/p/8407097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!