Springboot整合web开发

Posted 奋斗的小刀001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 























以上是关于Springboot整合web开发的主要内容,如果未能解决你的问题,请参考以下文章

全栈编程系列SpringBoot整合Shiro(含KickoutSessionControlFilter并发在线人数控制以及不生效问题配置启动异常No SecurityManager...)(代码片段

Springboot整合web开发

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

SpringBoot整合WEB开发--配置AOP

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

SpringBootWeb整合开发