Springboot整合web开发

一,整合 Servlet
1,通过注解扫描完成 Servlet 组件的注册
1.1 编写 servlet

 1 /**
 2  *
 3  * springboot整合servlet方式一
 4  * @author java
 5  *<servlet>
 6  *    <servlet-name>FirstServletController</servlet-name>
 7  *    <servlet-class>com.zzp.controller.FirstServletController</servlet-class>
 8  *</servlet>
 9  *<servlet-mapping>
10  *    <servlet-name>FirstServletController</servlet-name>
11  *    <url-pattern>/firstServlet</url-pattern>
12  *</servlet-mapping>
13  */
14 @WebServlet(name="FirstServletController",urlPatterns="/firstServlet")
15 public class FirstServletController extends HttpServlet{
16
17     @Override
18     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19         System.out.println("FirstServlet。。。。。。。。。。。。。");
20     }
21
22 }

1.2 编写启动类

/**
 *
 * springboot整合servlet方式一
 * @author java
 *
 */
@SpringBootApplication
@ServletComponentScan//在servlet启动时扫描@WebServlet这个注解
public class APP {

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

}

2,通过方法完成 Servlet 组件的注册

2.1 编写 servlet

 1 /**
 2  *
 3  * springboot整合servlet的方式二
 4  *
 5  * @author java
 6  *
 7  */
 8 public class SecondServlet extends HttpServlet{
 9
10     @Override
11     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12         System.out.println("secondServlet.................");
13     }
14
15 }

2.2 编写启动类

 1 /**
 2  *
 3  * springboot整合servlet的第二种方式
 4  * 如果第二种方式启动项目时,端口号被占用了,
 5  * 则在在src->main->resources目录下新建一个文件,名称为:application.properties
 6  * 设置端口号为server.port = 9527
 7  * @author java
 8  *
 9  */
10 @SpringBootApplication
11 public class AppSecond {
12
13     public static void main(String[] args) {
14         SpringApplication.run(AppSecond.class, args);
15     }
16
17     @Bean
18     public ServletRegistrationBean getServletRegistrationBean() {
19         ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
20         bean.addUrlMappings("/secondServlet");
21         return bean;
22     }
23 }

二,整合 Filter

1,通过注解扫描完成 Filter 组件的注册
1.1 编写Filter

 1 /**
 2  *
 3  * 方式一:通过注解扫描完成filter组件的注入
 4  * <filter>
 5  *     <filter-name></filter-name>
 6  *     <filter-class></filter-class>
 7  * </filter>
 8  * <filter-mapping>
 9  *     <filter-name></filter-name>
10  *     <url-pattern></url-pattern>
11  * </filter-mapping>
12  * @author java
13  *
14  */
15 //@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
16 @WebFilter(filterName="FirstFilter",urlPatterns="/firstServlet")
17 public class FirstFilter implements Filter{
18
19     @Override
20     public void init(FilterConfig filterConfig) throws ServletException {
21         // TODO Auto-generated method stub
22
23     }
24
25     @Override
26     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
27             throws IOException, ServletException {
28         System.out.println("开始进入filter");
29         chain.doFilter(request, response);
30         System.out.println("离开filter");
31     }
32
33     @Override
34     public void destroy() {
35         // TODO Auto-generated method stub
36
37     }
38
39 }
 1 /**
 2  *
 3  * springboot整合servlet方式一
 4  * @author java
 5  *<servlet>
 6  *    <servlet-name>FirstServletController</servlet-name>
 7  *    <servlet-class>com.zzp.controller.FirstServletController</servlet-class>
 8  *</servlet>
 9  *<servlet-mapping>
10  *    <servlet-name>FirstServletController</servlet-name>
11  *    <url-pattern>/firstServlet</url-pattern>
12  *</servlet-mapping>
13  */
14 @WebServlet(name="FirstServletController",urlPatterns="/firstServlet")
15 public class FirstServletController extends HttpServlet{
16
17     @Override
18     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19         System.out.println("FirstServlet。。。。。。。。。。。。。");
20     }
21
22 }

1.2 编写启动类

1 @SpringBootApplication
2 @ServletComponentScan
3 public class APP01 {
4
5     public static void main(String[] args) {
6         SpringApplication.run(APP01.class, args);
7     }
8 }

2,通过方法完成 Filter 组件的注册

2.1 编写 Filter

 1 public class SecondFilter implements Filter{
 2
 3     @Override
 4     public void init(FilterConfig filterConfig) throws ServletException {
 5         // TODO Auto-generated method stub
 6
 7     }
 8
 9     @Override
10     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
11             throws IOException, ServletException {
12         System.out.println("开始进入filter");
13         chain.doFilter(request, response);
14         System.out.println("离开filter");
15
16     }
17
18     @Override
19     public void destroy() {
20         // TODO Auto-generated method stub
21
22     }
23
24 }
 1 /**
 2  *
 3  * springboot整合servlet的方式二
 4  *
 5  * @author java
 6  *
 7  */
 8 public class SecondServlet extends HttpServlet{
 9
10     @Override
11     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
12         System.out.println("secondServlet.................");
13     }
14
15 }

2.2 编写启动类

 1 /**
 2  *
 3  * springboot通过方法注册filter的第二种方式
 4  * @author java
 5  *
 6  */
 7 @SpringBootApplication
 8 public class App02 {
 9
10     public static void main(String[] args) {
11         SpringApplication.run(App02.class, args);
12     }
13
14     @Bean
15     public ServletRegistrationBean getServletRegistrationBean() {
16         ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
17         bean.addUrlMappings("/secondServlet");
18         return bean;
19     }
20
21     @Bean
22     public FilterRegistrationBean getFilterRegistrationBean() {
23         FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
24         //bean.addUrlPatterns(new String[] {"*.do","*.jsp"});
25         bean.addUrlPatterns("/secondServlet");
26         return bean;
27     }
28 }

三,整合 Listener
1,通过注解扫描完成 Listener 组件的注册
1.1 编写 Listener

/**
*
* 方法一:通过注解扫描完成 Listener 组件的注册
* @author java
*<listener>
* <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener{

@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("listener....init....");
}

@Override
public void contextDestroyed(ServletContextEvent sce) {

}

}

1.2 编写启动类

 1 /**
 2  *
 3  * 方法一:通过注解扫描完成 Listener 组件的注册
 4  * @author java
 5  *
 6  */
 7 @SpringBootApplication
 8 @ServletComponentScan
 9 public class App01 {
10
11     public static void main(String[] args) {
12         SpringApplication.run(App01.class, args);
13     }
14
15 }

2. 通过方法完成 Listener 组件注册

2.1 编写 Listen

 1 /**
 2  *
 3  * springboot注册listener的方式二
 4  * @author java
 5  *
 6  */
 7 public class SecondListener implements ServletContextListener{
 8
 9     @Override
10     public void contextInitialized(ServletContextEvent sce) {
11         System.out.println("SecondListener...init...");
12     }
13
14     @Override
15     public void contextDestroyed(ServletContextEvent sce) {
16         // TODO Auto-generated method stub
17
18     }
19
20 }

2.2 编写启动类

 1 /**
 2  *
 3  * springboot注册listener的方式二
 4  * @author java
 5  *
 6  */
 7 @SpringBootApplication
 8 public class App02 {
 9     public static void main(String[] args) {
10         SpringApplication.run(App02.class, args);
11     }
12
13     @Bean
14     public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
15         ServletListenerRegistrationBean<SecondListener> bean = new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
16         return bean;
17     }
18 }

原文地址:https://www.cnblogs.com/zhangzhipeng001/p/9823418.html

时间: 2024-08-27 12:50:34

Springboot整合web开发的相关文章

SpringBoot整合WEB开发--(八)启动任务系统

简介: 有一些特殊的任务需要在系统启动时执行,例如配置文件的加载,数据库初始化等操作,如果没有使用SpringBoot,这些问题可以在Listener中解决.SpringBoot提供了两种解决方案:CommandLineRunner和ApplicationRunner,这两个差别主要体现在参数上. 1.CommandLineRunner SpringBoot项目在启动时会遍历所有的CommandLineRunner的实现类并调用其中的run方法,如果整个系统中有多个CommandLineRunn

SpringBoot整合WEB开发--(十)配置AOP

简介: SpringBoot框架中对AOP有很好的支持,简单AOP概念: JoinPoint(连接点):类里面可以被增强的方法即为连接点,例如,想修改哪个方法的功能,那么该方法就是一个连接点. Pointcut(切入点):对JoinPoint进行拦截的定义即为切入点,例如拦截所有insert开始的方法,这个定义即为切入点. Advice(通知):拦截到Joinpoint之后要做的事就是通知,分为前置,后置,异常,环绕,返回通知. Aspect(切面):Pointcut和Advice的结合. Ta

JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的JavaSE/EE full-stack(一站式)轻量级开源框架. 所谓分层: SUN提供的EE的三层结构:web层.业务层.数据访问层(也称持久层,集成层). Struts2是web层基于MVC设计模式框架. Hibernate是持久的一个ORM的框架. 所谓一站式:Spring框架有对三层的每层解决方案.

Spring整合web开发(6)

正常整合Servlet和Spring没有问题的但是每次执行Servlet的时候加载Spring配置,加载Spring环境. 解决办法:在Servlet的init方法中加载Spring配置文件? 当前这个Servlet可以使用,但是其他的Servlet的用不了了!!! 将加载的信息内容放到ServletContext中.ServletContext对象时全局的对象.服务器启动的时候创建的.在创建ServletContext的时候就加载Spring的环境. ServletContextListene

SpringBoot的Web开发

Web开发是开发中至关重要的一部分,web开发的核心内容主要包括servelet容器和SpringMVC. 1.SpringBoot的Web开发支持. SpringBoot提供了spring-boot-starter-web为web开发予以支持,spring-boot-starter-web提供了内嵌的Tomcat以及SpringMVC的依赖 而web相关的自动配置存储在spring-boot-autoconfigure.jar的org.srpingframework.boot.autoconf

4.SpringBoot的web开发1

一.回顾 好的,同学们,那么接下来呢,我们开始学习SpringBoot与Web开发,从这一章往后,就属于我们实战部分的内容了: 其实SpringBoot的东西用起来非常简单,因为SpringBoot最大的特点就是自动装配. 使用SpringBoot的步骤: 创建一个SpringBoot应用,选择我们需要的模块,SpringBoot就会默认将我们的需要的模块自动配置好 手动在配置文件中配置部分配置项目就可以运行起来了 专注编写业务代码,不需要考虑以前那样一大堆的配置了. 要熟悉掌握开发,之前学习的

Spring Boot 整合 Web 开发

这一节我们主要学习如何整合 Web 相关技术: Servlet Filter Listener 访问静态资源 文件上传 文件下载 Web三大基本组件分别是:Servlet,Listener,Filter.正常来说一旦我们用了框架,这三个基本就用不上了,Servlet 被 Controller 代替,Filter 被拦截器代替.但是可能在一些特殊的场景下不得不使用这三个基本组件时,Spring Boot 中要如何去引用呢?下面我们来一起学习一下. Spring Boot 集成了 Servlet 容

SpringBoot与Web开发

web开发1).创建SpringBoot应用,选中我们需要的模块:2).SpringBoot已经默认将这些场景已经配置好了,只需要在配置文件中指定少量配置就可以运行起来3).自己编写业务代码: 自动配置原理?这个场景SpringBoot帮我们配置了扫码?能不能修改?能不能改哪些配置?能不能扩展?xxxxxxAutoConfiguration:帮我们给容器中自动配置组件:xxxProperties:配置类来 封装配置文件的内容: 2.SpringBoot对静态资源的 映射规则 @Configura

【SpringBoot】Web开发

一.简介 1.1 引入SpringBoot模块 1.2 SpringBoot对静态资源的映射规则 二.模版引擎 2.1 简介 2.2 引入thymeleaf 2.3 Thymeleaf使用 一.简介 1.1 引入SpringBoot模块 在介绍Web开发模块之前,先总结一下SpringBoot中如何引入某一个模块,我们知道,SpringBoot将功能模块封装为一个个的Starter : 1).创建SpringBoot应用,选中我们需要的模块; 2).SpringBoot已经默认将这些场景配置好了