zoukankan      html  css  js  c++  java
  • 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文件中的内容即可,其他内容参见方案一

  • 相关阅读:
    Java学习笔记——继承、接口、多态
    Java学习笔记,前两章总结
    网络攻防第十一周总结
    第十一周网络攻防作业
    第十周网络作业
    第九周网络攻防作业
    第八周网络攻防作业
    第七周网络攻防作业
    第六周网络攻防作业
    第五周网络攻防作业
  • 原文地址:https://www.cnblogs.com/studyDetail/p/7001774.html
Copyright © 2011-2022 走看看