Spring Boot系列6-SpringBoot中使用servlet

介绍在SpringBoot中如何使用servlet

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency> 

定义类MyServlet继承HttpServlet

@WebServlet标记servlet,urlPatterns配置映射路径

重写doGet方法

@WebServlet(urlPatterns = "/my/servlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        resp.getWriter().print("hello this is MyServlet!");
    }
}

配置启动类

@ServletComponentScan(basePackages = “com.tiankonglanlande.cn.springboot.servlet.servlet”)这个注解作用是 配置要扫描servlet的位置,springboot会自动将包下的servlet注入


@SpringBootApplication
@ServletComponentScan(basePackages = "com.tiankonglanlande.cn.springboot.servlet.servlet")
public class ServletApplication {

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

异步的servlet

配置@WebServlet的属性asyncSupported = true即可

/**
 * @author 天空蓝蓝的
 */
@WebServlet(urlPatterns = "/my/ayncservlet",asyncSupported = true)
public class MyAyncServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);

        AsyncContext asyncContext = req.getAsyncContext();
        asyncContext.start(()->{
            try {
                resp.getWriter().print("hello this is MyAyncServlet!");
                asyncContext.complete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}
 

测试

在浏览器访问:http://localhost:8080/my/servlet

输出:hello this is MyServlet!

在浏览器访问:http://localhost:8080/my/ayncservlet

输出:hello this is MyAyncServlet!

可能遇到的问题

1.HTTP method GET is not supported by this URL 解决方法:doGet方法中去掉super.doGet()方法调用

2.java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false) 原因:(1)将req.startAsync()错写成req.getAsyncContext(); (2)asyncContext.complete()需要在任务完成后调用

源码下载链接

作者:天空蓝蓝的,版权所有,欢迎保留原文链接进行转载:) 关注我们的公众号了解更多

原文链接:https://www.lskyf.xyz/spring-boot/2018/11/11/SpringBoot系列6-SpringBoot中使用servlet

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以使用属性`spring.servlet.multipart.max-request-size`来设置最大请求大小。该属性的值可以使用后缀"MB"或"KB"来表示兆字节或千字节。例如,`spring.servlet.multipart.max-request-size=10MB`表示最大请求大小为10兆字节。\[3\] #### 引用[.reference_title] - *1* [Spring Boot的max-http-header-size配置](https://blog.csdn.net/tianzhonghaoqing/article/details/126519374)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [SpringBoot 下文件上传大小问题解决](https://blog.csdn.net/leeahuamsg/article/details/81913286)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [记一个spring boot max-file-size的变化](https://blog.csdn.net/wang124454731/article/details/84872638)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值