20. Spring Boot Servlet【从零开始学Spring Boot】
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>");
-
- }
-
- }
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
购买完整视频,请前往:http://www.mark-to-win.com/TeacherV2.html?id=287