springboot注册servlet,Filter,Listener
来源:互联网 发布:淘宝小铺货源 编辑:程序博客网 时间:2024/05/21 09:05
- 注解注册
SpringBootApplication 上使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。
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(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) 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"); 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>"); }}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); }}
- 代码注册
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(QQ:412887952) * @version v.0.1 *///这个不需要添加.//@WebServlet(urlPatterns="/myServlet1/*", description="Servlet的说明")publicclass MyServlet1 extends HttpServlet{ privatestaticfinallongserialVersionUID = 1L; @Override protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<"); doPost(req, resp); } @Override protectedvoid doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, 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>这是:MyServlet1</h1>"); out.println("</body>"); out.println("</html>"); }}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 * * 所以大家不要被一些文档误导了,让自己很迷茫了,希望本文章对您有所启发; * * @author Angel(QQ:412887952) * @version v.0.1 */@SpringBootApplicationpublic class App { /** * 注册Servlet.不需要添加注解:@ServletComponentScan * @return */ @Bean public ServletRegistrationBean MyServlet1(){ return new ServletRegistrationBean(new MyServlet1(),"/myServlet/*"); } public static void main(String[] args) { SpringApplication.run(App.class, args); }}
原文:http://412887952-qq-com.iteye.com/blog/2292472
阅读全文
0 0
- springboot注册servlet,Filter,Listener
- 第四十二章 SpringBoot注册Servlet/Filter/Listener
- springBoot配置servlet,filter,listener
- springboot实战之注册filter和listener
- SpringBoot中Servlet&Listener&Filter的应用
- SpringBoot--添加配置Servlet,Filter,listener
- 在SpringBoot中使用 Servlet Filter Listener
- springboot 使用Servlet,Filter,Listener的方法
- SpringBoot通过代码注册Servlet、Fillter、Listener
- SpringBoot通过代码注册Servlet、Fillter、Listener
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
- SpringBoot初始教程之Servlet、Filter、Listener配置
- SpringBoot初始教程之Servlet、Filter、Listener配置
- Servlet Filter Servlet Listener
- 18.玩转Spring Boot 注册Servlet、Filter、Listener
- Servlet3.0动态注册Servlet、Filter和Listener
- Servlet、Filter和Listener
- API学习StringBuffer
- git-osc-android客户端之源码分析4
- maven之学习之路(三)
- MapReduce:详解Shuffle过程
- 服务器磁盘满了,mysql写入数据报错table xx is full
- springboot注册servlet,Filter,Listener
- 函数指针定义方式
- CentOS7搭建Apache服务器
- 趣味c 编程之查找凶手
- 八、mcg-helper升级V1.1.0-Alpha版本
- WebSocket安卓客户端实现详解(三)--服务端主动通知
- ubuntu16.04编译dlib for Android
- stm32发送16进制的数组总结
- ubuntu14.04_YouKu安装Adobe_flash