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文件中的内容即可,其他内容参见方案一

  • 相关阅读:
    Bitcoin core核心客户端在CentOS7上的安装和配置
    Struts的标签及JSTL和EL表达式的使用总结
    Java反编译
    Tomcat 各版本 配置SSI服务实现html模块化实现#include virtual="static/_header.html"
    33
    SQL Server Management Studio无法记住密码
    SQL Server 创建角色和账号
    JS 取出DataGrid 列
    关于HTTP协议的小实验
    DNS服务操作小实验
  • 原文地址:https://www.cnblogs.com/studyDetail/p/7001774.html
Copyright © 2011-2022 走看看