• SpringBoot框架下使用Servlet


    SpringBoot框架下使用Servlet

    1. 创建一个Servlet继承HttpServlet
    2. 在web.xml配置文件中使用servlet servlet-mapping

    1. 第一种方式:注解的方式—@WebServlet,@ServletComponentScan

    @WebServlet: 配置Servlet请求的URL
    @ServletComponentScan:扫描指定包

    SpringBootApplication 上使用@ServletComponentScan 注解后
    Servlet可以直接通过@WebServlet注解自动注册

    1.1 创建一个Servlet类继承HttpServlet 类—@WebServlet

    src/main/java/com/guo/springboot/servlet/MyServlet.java

    package com.guo.springboot.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet(urlPatterns = "/myservlet")  //指定Servlet所请求的路径
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("My SpringBoot Servlet-1");
            resp.getWriter().flush();
            resp.getWriter().close();
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    1.2 程序入口类添加注解—@ServletComponentScan

    src/main/java/com/guo/springboot/Application.java

    package com.guo.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication //开启spring配置
    @ServletComponentScan(basePackages = "com.guo.springboot.servlet")  //扫描Servlet所在包
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.3 结果截图

    在这里插入图片描述

    2. 第二种方式:配置类注解组件

    2.1 创建一个Servlet类继承HttpServlet 类—@WebServlet

    和第一种方法相同创建一个Servlet类继承HttpServlet类,实现doGet,doPost方法

    src/main/java/com/guo/springboot/servlet/MyServlet.java

    package com.guo.springboot.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    @WebServlet(urlPatterns = "/myservlet")  //指定Servlet所请求的路径
    public class MyServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("My SpringBoot Servlet-2");
            resp.getWriter().flush();
            resp.getWriter().close();
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req,resp);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    @Configuration //该注解将此类定义为配置类,(相当于一个xml配置文件)

    2.2 创建配置类相当于(xml配置文件)

    src/main/java/com/guo/springboot/config/ServletConfig.java

    package com.guo.springboot.config;
    
    import com.guo.springboot.servlet.MyServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration   //该注解将此类定义为配置类,(相当于一个xml配置文件)
    public class ServletConfig {
        //@Bean是一个方法级别上的注解,主要用于在配置类里
        //相当于一个
        // 
        //     
        // 
        @Bean
        public ServletRegistrationBean myServletRegistrationBean(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
            return servletRegistrationBean;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.3 运行结果截图

    在这里插入图片描述

    3. 扩展PrintWriter

    Servlet的时候用到了resp.getWriter()来给前端传数据

    PrintWriter 是有两个方法对页面进行传值的,首先说一下两个方法的区别

    • write():紧支持输出字符类型,字符,字符数组字符串等
    • print():可以使各种类型,包括object,通过默认编码格式转换成bytes字节形式,这些字节都是通过write(int c)方法让然后被输出 print可以写入对象,write不可以。

    PrintWriter对象的flush()和close()方法说明:

    • flush()将缓冲区的数据强制输出,用于清空缓冲区,若直接调用close()方法,则可能会丢失缓冲区的数据。所以通俗来讲它起到的是刷新的作用。
    • close()用于关闭数据流
      原文链接:https://blog.csdn.net/qq_37745470/article/details/99598276
  • 相关阅读:
    Vue3-shallowRef与shallowReactive
    Bootstrap的宽度和高度的设置(相对于父元素的宽度和高度、相对于视口的宽度和高度)
    C#用API读取.ini非中英文路径失败问题
    渐进式量产进阶!卡车自动驾驶进入商业化新周期
    leetcode 5229: 拼接数组的最大分数
    对话张璐:硅谷正在追逐两大赛道创新,融资降温但技术商业化更快了
    Spring Security认证器实现
    ssm信息安全资讯网站毕业设计-附源码191651
    计蒜客T1375 百钱买百鸡(四)
    VMware虚拟机安装黑苹果步骤与常见问题,VMware16,MacOS12.01(Moterey)
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126091449