• (20)Spring Boot Servlet【从零开始学Spring Boot】


    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 ServletFilterListenerInterceptor 等等。

    当使用Spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册ServletFilterServlet规范的所有监听器(如HttpSessionListener监听器)。 
    Spring boot
    的主 Servlet DispatcherServlet,其默认的url-pattern“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

    spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(FilterListener也是如此)。 
    一、代码注册通过ServletRegistrationBean FilterRegistrationBean ServletListenerRegistrationBean 获得控制。 
    也可以通过实现 ServletContextInitializer 接口直接注册。

    二、在 SpringBootApplication 使用@ServletComponentScan注解后,ServletFilterListener 可以直接通过 @WebServlet@WebFilter@WebListener 注解自动注册,无需其他代码。

    这里我们新建一个spring-boot-hello2 java工程进行测试;这里不过多进行说明,如果这个还不会的话,请回到上上上一章进行查看。

     

    通过代码注册Servlet示例代码:

    com.kfit.servlet.MyServlet1

    package com.kfit.servlet;

     

    import java.io.IOException;

    import java.io.PrintWriter;

     

    import javax.servlet.ServletException;

    import javax.servlet.annotation.WebServlet;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

     

    /**

     *

     * @author Angel(QQ:412887952)

     * @version v.0.1

     */

    //这个不需要添加.

    //@WebServlet(urlPatterns="/myServlet1/*", description="Servlet的说明")

    publicclass MyServlet1 extends HttpServlet{

       

        privatestaticfinallongserialVersionUID = 1L;

     

        @Override

        protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");

            doPost(req, resp);

        }

     

        @Override

        protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");

            resp.setContentType("text/html"); 

            PrintWriter out = resp.getWriter(); 

            out.println("<html>"); 

            out.println("<head>"); 

            out.println("<title>Hello World</title>"); 

            out.println("</head>"); 

            out.println("<body>"); 

            out.println("<h1>这是:MyServlet1</h1>"); 

            out.println("</body>"); 

            out.println("</html>");

        }

    }

     

    com.kfit.App中注册:

    package com.kfit;

     

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.context.embedded.ServletRegistrationBean;

    import org.springframework.boot.web.servlet.ServletComponentScan;

    import org.springframework.context.annotation.Bean;

     

    import com.kfit.servlet.MyServlet1;

     

    /**

     *

     *

     * 大家也许会看到有些demo使用了3个注解: @Configuration

     *

     * @EnableAutoConfiguration

     * @ComponentScan

     *

     *                                 其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,

     *

     *                等价于以默认属性使用@Configuration

     *                @EnableAutoConfiguration@ComponentScan

     *

     * 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;

     *

     * @author Angel(QQ:412887952)

     * @version v.0.1

     */

    @SpringBootApplication

    public class App {

            

             /**

              * 注册Servlet.不需要添加注解:@ServletComponentScan

              * @return

              */

             @Bean

             public ServletRegistrationBean MyServlet1(){

                       return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");

             }

            

             public static void main(String[] args) {

                       SpringApplication.run(App.class, args);

             }

    }

     

    右键Run As Java Application进行访问http://127.0.0.1:8080/myServlet1

     

     

    使用注解注册Servlet示例代码

    com.kfit.servlet.MyServlet2.java

    package com.kfit.servlet;

     

    import java.io.IOException;

    import java.io.PrintWriter;

     

    import javax.servlet.ServletException;

    import javax.servlet.annotation.WebServlet;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

     

    /**

     *

     * @author Angel(QQ:412887952)

     * @version v.0.1

     */

    @WebServlet(urlPatterns="/myServlet2/*", description="Servlet的说明")

    public class MyServlet2 extends HttpServlet{

          

           private static final long serialVersionUID = 1L;

     

           @Override

        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");

            doPost(req, resp);

        }

     

        @Override

        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");

            resp.setContentType("text/html"); 

            PrintWriter out = resp.getWriter(); 

            out.println("<html>"); 

            out.println("<head>"); 

            out.println("<title>Hello World</title>"); 

            out.println("</head>"); 

            out.println("<body>"); 

            out.println("<h1>这是:myServlet2</h1>"); 

            out.println("</body>"); 

            out.println("</html>");

        }

    }

     

    SpringBootSampleApplication.java

    package com.kfit;

     

    import org.springboot.sample.servlet.MyServlet;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.context.embedded.ServletRegistrationBean;

    import org.springframework.boot.web.servlet.ServletComponentScan;

    import org.springframework.context.annotation.Bean;

    import org.springframework.web.servlet.DispatcherServlet;

     

    @SpringBootApplication

    @ServletComponentScan//这个就是扫描相应的Servlet;

    public class SpringBootSampleApplication {

     

        public static void main(String[] args) {

            SpringApplication.run(SpringBootSampleApplication.class, args);

        }

    }

     

    访问http://127.0.0.1:8080/myServlet2

  • 相关阅读:
    C# DataGridView隔行显示不同的颜色
    C#也能动态生成Word文档并填充数据
    [zz CSDN]上班族饮食十大“夺命”恶习
    VB设置网卡的IP地址
    图片按钮带检测
    让IIS支持FLV
    每天看超过500封简历的HR详谈求职之道
    鼠标移动,背景变色
    Panabit 个人理解摘要
    QcCQ宠大乐斗企鹅鉴定及评分标准[测试版]
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6147124.html
Copyright © 2020-2023  润新知