Spring Boot 中的Servlet简单使用

2025-05-29 0 70

当使用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 注解自动注册,无需其他代码。

1.通过代码注册Servlet示例代码

1).SpringBootSimpleApplication.Java类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
package com.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

import org.springframework.context.annotation.Bean;

import com.example.servlet.MyServlet;

@SpringBootApplication

public class SpringBootSimpleApplication {

/**

* 使用代码注册Servlet(不需要@ServletComponentScan注解)

*/

@Bean

public ServletRegistrationBean servletRegistrationBean() {

return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet

}

public static void main(String[] args) {

SpringApplication.run(SpringBootSimpleApplication.class, args);

}

}

2).MyServlet.java类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29
package com.example.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet{

@Override

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

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

doPost(req, resp);

}

@Override

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

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

resp.setContentType("text/html");

resp.setCharacterEncoding("utf-8");

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>大家好,我的名字叫Servlet</h1>");

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

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

}

}

2.使用注解注册Servlet示例代码

1).SpringBootSimpleApplication.java类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
package com.example;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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

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

import org.springframework.context.annotation.Bean;

import com.example.servlet.MyServlet;

@SpringBootApplication

@ServletComponentScan

public class SpringBootSimpleApplication {

/**

* 使用代码注册Servlet(不需要@ServletComponentScan注解)

*/

@Bean

public ServletRegistrationBean servletRegistrationBean() {

return new ServletRegistrationBean(new MyServlet(), "/st/*");// ServletName默认值为首字母小写,即myServlet

}

public static void main(String[] args) {

SpringApplication.run(SpringBootSimpleApplication.class, args);

}

}

2).MyServlet2.java类:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
package com.example.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;

// 不指定name的情况下,name默认值为类全路径,即com.example.servlet.MyServlet2

@WebServlet(urlPatterns="/st/myservlet2", description="Servlet的说明")

public class Myservlet2 extends HttpServlet{

@Override

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

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

doPost(req, resp);

}

@Override

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

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

resp.setContentType("text/html");

resp.setCharacterEncoding("utf-8");

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>大家好,我的名字叫Servlet2</h1>");

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

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

}

}

使用 @WebServlet 注解,其中可以设置一些属性。

3.访问结果

Spring Boot 中的Servlet简单使用

4.DispatcherServlet默认拦截

DispatcherServlet 默认拦截“/”,MyServlet 拦截“/st/*”,MyServlet2 拦截“/st/myservlet”,那么在我们访问 http://localhost:8080/st/myservlet2 的时候系统会怎么处理呢?如果访问 http://localhost:8080/st/abc的时候又是什么结果呢?其结果是“匹配的优先级是从精确到模糊,复合条件的Servlet并不会都执行”。

既然系统DispatcherServlet 默认拦截“/”,那么我们是否能做修改呢,答案是肯定的,我们在SpringBootSampleApplication中添加代码:

?

1

2

3

4

5

6

7

8

9

10

11
/**

* 修改DispatcherServlet默认配置

*/

@Bean

public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {

ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);

registration.getUrlMappings().clear();

registration.addUrlMappings("*.do");

registration.addUrlMappings("*.json");

return registration;

}

可以通过注入DispatcherServlet 然后用ServletRegistrationBean包裹一层 动态的加上一些初始参数。

原文链接:http://www.cnblogs.com/web424/p/6755950.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring Boot 中的Servlet简单使用 https://www.kuaiidc.com/117029.html

相关文章

发表评论
暂无评论