• springboot整合servlet


    在idea新建项目

     

    这个是pom.xml文件需要添加的依赖包

    <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

     新建一个servlet包

     

    在这个包下面新建FirstServlet类

     

    FirstServlet.java 类

    package com.example.gong02.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;
    
    /*
       * springboot 整合servlet方式一
       *
       <servlet>
       * <servlet-name>FirstServlet</servlet-name>
       * <servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class>
       *</servlet>
       * *
       <servlet-mapping>
       * <servlet-name>FirstServlet</servlet-name>
       * <url-pattern>/first</url-pattern>
       *</servlet-mapping>
       * */
    @WebServlet(name="FirstServlet",urlPatterns="/first")
    public class FirstServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("FistServlet..........");

    } }

    编写启动类

     App.java类

    package com.example.gong02;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    /**
    * SpringBoot 整合 Servlet 方式一
    * *
    */

    @SpringBootApplication

    @ServletComponentScan
    //在 springBoot 启动时会扫描@WebServlet, 并将该类实例化 

    public class App {
    public static void main(String[] args) {
    SpringApplication.run(App.
    class, args);
    }
    }

    运行App.java

     在浏览器输入

    终端打印

    下面介绍servlet的第二种整合方式

    新建SecondServlet类

     

    package com.example.gong02.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     * SpringBoot 整合 Servlet 方式二
     *
     */
    public class SecondServlet extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("SecondServlet.......");
        }
    }

    编写启动类App2.java

     App2.java类

    package com.example.gong02;
    
    import com.example.gong02.servlet.SecondServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    /**
     * SpringBoot 整合 Servlet 方式二
     * *
       */
    @SpringBootApplication
    public class App2 {
      public static void main(String[] args){
          SpringApplication.run(App2.class, args);
      }
    
        @Bean
        public ServletRegistrationBean getServletRegistrationBean(){
            ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
            bean.addUrlMappings("/second");
            return bean;
        }
    }

    运行

     

     在浏览器输入

    可以看到终端打印出来了

     

     注意了,大家在运行方式二的时候可能会出现8080端口被占用的情况导致tomcat无法启动,遇到这种情况可以重启一下idea就能解决了

  • 相关阅读:
    sql except 用法,找两个表中非共同拥有的
    ‘堆’出你的洪荒之力
    原来你是个这样的JVM
    变形词
    54题
    最大对称子数组
    java 线程之间通信以及notify与notifyAll区别。
    大型网站架构系列:消息队列
    剑指offer第10题
    & 和 && 区别
  • 原文地址:https://www.cnblogs.com/braveym/p/11298371.html
Copyright © 2020-2023  润新知