Spring Boot入门——web相关配置

1、Servlet

  引用HttpServlet接口,采用原生的Servlet进行请求响应

 

2、Listener

 

  引用ServletContextListener,常用于Web缓存

 

3、Filter

 

  引用Filter接口,常用于认证、日志、令牌等

4、实现

 

  方案一:采用原生的Servlet3.0注解进行配置 @WebServlet、@WebListener、@WebFilter

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(name="indexServlet", urlPatterns="/index")
public class IndexServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------------IndexServlet doGet--------------");
        resp.getWriter().print("hello world!!!");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("-------------IndexServlet doPost--------------");
        this.doGet(req, resp);
    }
}
IndexServlet Code
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class IndexListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("-------------IndexListener contextDestroyed--------------");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("-------------IndexListener contextInitialized--------------");    
    }

}
IndexListener Code
IndexFilter Code

  方案二:采用自己SpringBoot配置bean的方式进行配置

    SpringBoot提供三种bean:FilterRegistrationBean、ServletRegistrationBean、ServletListenerRegistrationBean分别对应原生的Filter、Servlet、Listener

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.wyl.common.IndexFilter;
import com.wyl.common.IndexListener;
import com.wyl.common.IndexServlet;

/**
 * Hello world!
 *
 */
@SpringBootApplication
//@ServletComponentScan
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
//注册三个Bean即可 @Bean
public FilterRegistrationBean indexFilter(){ FilterRegistrationBean filter = new FilterRegistrationBean(new IndexFilter()); filter.addUrlPatterns("/"); return filter; } @Bean public ServletRegistrationBean indexServlet(){ ServletRegistrationBean servlet = new ServletRegistrationBean(new IndexServlet()); servlet.addUrlMappings("/index"); return servlet; } @Bean public ServletListenerRegistrationBean indexListener(){ ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(new IndexListener()); return listener; } }

 

  注:只需要修改App.class文件中的内容即可,其他内容参见方案一

posted @ 2017-06-13 15:59  Miss_wang  阅读(519)  评论(0编辑  收藏  举报