zoukankan      html  css  js  c++  java
  • springBoot 整合servlet

    一:用idea 创建 springboot 项目:

    详情请参考:《使用IDEA创建一个springboot项目



    二:具体代码内容;

    image


    一: FirstServlet


    package com.alancode.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;
    
    /**
     * @program: springBootAddServlet
     * @description: FristServlet
     *  一:传统的spring 框架增加 servlet 的配置方式
     *   <servlet>
     *       <servlet-name>FirstServlet</servlet-name>
     *       <servlet-class>com.alancode.springboot.Servlet.FirstServlet</servlet-class>
     *   </servlet>
     *   <servlet-mapping>
     *       <servlet-name>FirstServlet</servlet-name>
     *       <url-pattern>/frist</url-pattern>
     *   </servlet-mapping>
     *
     */
    /***springBoot  配置 servlet 的配置代码
     * @author Alan_Liu***/
    @WebServlet(name="FirstServlet",urlPatterns = "/frist")
    public class FirstServlet extends HttpServlet {
        /**
         *
         *
         *@Param: [req, resp]
         *@return
         *@throws
         *@author Alan_Liu
         *@date   2021/2/14 23:22
         *
         **/
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
           //super.doGet(req,resp);
    		System.out.println("------------springBoot 整合 servlet 方式一:注解配置方式------------------------------------------ -----------");
    		System.out.println("-----------项目启动时候加载 @WebServlet 注解下的FirstServlet 类 - ---------------------------------");
    		System.out.println("-----------当进行http://localhost:8080/frist 的浏览器地址访问后,进输出该打印内容 - ----------------------");
    	}
    }



    二:SecondServlet


    package com.alancode.springboot.Servlet;
    /**
     * @author Alan_liu
     * @Create 2021/2/14 23:46
     */
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    /**
     *@program: springBootAddServlet
     *@description: SecondServlet
     *@author: Alan_Liu
     *@create: 2021-02-14 23:46
     */
    public class SecondServlet extends HttpServlet {
    
    
    	@Override
    	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		//super.doGet(req,resp);
    		System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------");
    		System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类  - ---------------------------------");
    		System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------");
    
    	}
    }


    三:SpringBootAddServletApplication


    package com.alancode.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    /**
     * @author Alan_Liu
     */
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    @ServletComponentScan
    public class SpringBootAddServletApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringBootAddServletApplication.class, args);
    	}
    
    }
    
    /***
     *
     *
     **注意:
     **关于编写启动器需要注意的问题。
     **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。
     **但是不能把该文件放到controller平行级的包或者其子包下。
     * ServletComponentScan 含义:
     *   在springboot 启动时候进行扫描@webServlet注解的所有servlet类。并将该类实例化
     *
     * ***/
    
    


    四:SpringBootAddServletApplication2

    package com.alancode.springboot;
    
    import com.alancode.springboot.Servlet.SecondServlet;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    /**
     * @author Alan_Liu
     */
    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    public class SpringBootAddServletApplication2 {
    
    	public static void main(String[] args) {
    		SpringApplication.run(SpringBootAddServletApplication2.class, args);
    	}
    	@Bean
    	public ServletRegistrationBean getServletRegistrationBean(){
    		ServletRegistrationBean bean=new ServletRegistrationBean(new SecondServlet());
    		bean.addUrlMappings("/second");
    		System.out.println("------------springBoot 整合 servlet 方式二: ------------------------------------------ -----------");
    		System.out.println("-----------项目启动SpringBootAddServletApplication2时候加载声明初始化SecondServlet 类  - ---------------------------------");
    		System.out.println("-----------当进行http://localhost:8080/second 的浏览器地址访问后,进输出该打印内容 - ----------------------");
    
    		return  bean;
    	}
    }
    
    /***
     *
     *
     **注意:
     **关于编写启动器需要注意的问题。
     **启动器存放位置:启动器可以和controller 位于同一个包 package下,或者位于controller的上一级包。
     **但是不能把该文件放到controller平行级的包或者其子包下。
     * ServletComponentScan 含义:
     *   在springboot 启动时候进行扫描@webServlet注解的所有servlet类。并将该类实例化
     *
     * ***/
    
    


    五:pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.AlanCode.springBoot</groupId>
        <artifactId>springBootAddServlet</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>springBootAddServlet</name>
        <description>springBootAddServlet</description>
        <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>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
    学问:纸上得来终觉浅,绝知此事要躬行
    为事:工欲善其事,必先利其器。
    态度:道阻且长,行则将至;行而不辍,未来可期
    转载请标注出处!
  • 相关阅读:
    编译duilib遇到问题:Error C2371 "IDispatchEx重定义;不同的基类型"
    初尝DirectUI
    ms-onlinetest-question02
    ms-onlinetest-question3
    都是申请空间后不赋初值惹的祸..
    CString接受返回的char*字符串后成为乱码
    CL.exe @C:Users upAppDataLocalTemp mpc8fc399365e34f739eff6191a0c9acde.rsp”。存储控制块地址无效
    Visual Studio Ultimate 2012 静态激活密钥
    如何写入和获取软件的版本信息(VS环境下)
    mt.exe : general error c101008a: Failed to save the updated manifest to the file "DebugResource.dll.embed.manifest". Bpo
  • 原文地址:https://www.cnblogs.com/ios9/p/14403055.html
Copyright © 2011-2022 走看看