20. Spring Boot Servlet【从零开始学Spring Boot】

【视频 & 交流平台】

à SpringBoot视频

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à SpringCloud视频

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot源码

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

Web开发使用 Controller基本上可以完成大部分需求,但是我们还可能会用到 Servlet、Filter、Listener、Interceptor 等等。

当使用Spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。 
Spring boot 的主 Servlet 为DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)。 


一、代码注册通过ServletRegistrationBean、 FilterRegistrationBean和ServletListenerRegistrationBean 获得控制。 
也可以通过实现 ServletContextInitializer 接口直接注册。

二、在SpringBootApplication 使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

这里我们新建一个spring-boot-hello2java工程进行测试;这里不过多进行说明,如果这个还不会的话,请回到上上上一章进行查看。

 

通过代码注册Servlet示例代码:

com.kfit.servlet.MyServlet1:

package com.kfit.servlet;



import java.io.IOException;

import java.io.PrintWriter;



import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



/**

 *

 *@author Angel(公众号:SpringBoot)

 *@version v.0.1

 */

//这个不需要添加.

//@WebServlet(urlPatterns="/myServlet1/*", description="Servlet的说明")

public class MyServlet1 extends HttpServlet{

      

       private static final long serialVersionUID = 1L;



       @Override

   protected void doGet(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException {

        System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");

        doPost(req,resp);

   }



   @Override

   protected void doPost(HttpServletRequestreq, HttpServletResponse resp) throwsServletException, IOException {

        System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");

        resp.setContentType("text/html"); 

        PrintWriter out = resp.getWriter(); 

        out.println("<html>"); 

        out.println("<head>"); 

        out.println("<title>HelloWorld</title>"); 

        out.println("</head>"); 

        out.println("<body>"); 

        out.println("<h1>这是:MyServlet1</h1>"); 

        out.println("</body>"); 

        out.println("</html>");

   }

}

 

com.kfit.App中注册:

package com.kfit;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.embedded.ServletRegistrationBean;

import org.springframework.boot.web.servlet.ServletComponentScan;

import org.springframework.context.annotation.Bean;

 

import com.kfit.servlet.MyServlet1;

 

/**

 *

 *

 *大家也许会看到有些demo使用了3个注解:@Configuration;

 *

 *@EnableAutoConfiguration

 * @ComponentScan

 *

 *                        其实:@SpringBootApplication申明让spring boot自动给程序进行必要的配置,

 *

 *               等价于以默认属性使用@Configuration,

 *                @EnableAutoConfiguration和@ComponentScan

 *

 *所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发;

 *

 * @authorAngel(QQ:412887952)

 * @version v.0.1

 */

@SpringBootApplication

public class App {

      

       /**

        *注册Servlet.不需要添加注解:@ServletComponentScan

        * @return

        */

       @Bean

       publicServletRegistrationBean MyServlet1(){

              return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*");

       }

      

       public static void main(String[] args) {

              SpringApplication.run(App.class,args);

       }

}

 

右键Run As JavaApplication进行访问http://127.0.0.1:8080/myServlet1

 

 

使用注解注册Servlet示例代码

com.kfit.servlet.MyServlet2.java

package com.kfit.servlet;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 *

 * @authorAngel(QQ:412887952)

 * @version v.0.1

 */

@WebServlet(urlPatterns="/myServlet2/*",description="Servlet的说明")

public class MyServlet2 extends HttpServlet{

      

       private static final long serialVersionUID = 1L;

 

       @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

       System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");

        doPost(req, resp);

    }

 

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {

       System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");

        resp.setContentType("text/html"); 

        PrintWriter out =resp.getWriter(); 

       out.println("<html>"); 

       out.println("<head>"); 

       out.println("<title>Hello World</title>"); 

       out.println("</head>"); 

        out.println("<body>"); 

       out.println("<h1>这是:myServlet2</h1>"); 

       out.println("</body>"); 

       out.println("</html>");

    }

}

 

SpringBootSampleApplication.java
 

package com.kfit;

 

import org.springboot.sample.servlet.MyServlet;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.embedded.ServletRegistrationBean;

import org.springframework.boot.web.servlet.ServletComponentScan;

import org.springframework.context.annotation.Bean;

import org.springframework.web.servlet.DispatcherServlet;

 

@SpringBootApplication

@ServletComponentScan//这个就是扫描相应的Servlet包;

public class SpringBootSampleApplication {

 

    public static void main(String[]args) {

        SpringApplication.run(SpringBootSampleApplication.class, args);

    }

}

访问http://127.0.0.1:8080/myServlet2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟纤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值