springBoot整合web开发
springBoot整合Servlet
注解方式
1.使用idea创建springboot项目
2.编写一个servlet
/**
*SpringBoot 整合 Servlet 方式一 *
*<servlet>
* <servlet-name>FirstServlet</servlet-name>
* <servlet-class>com.bjsxt.servlet.FirstServlet</servlet-class> *</servlet> *
*<servlet-mapping>
* <servlet-name>FirstServlet</servlet-name>
* <url-pattern>/first</url-pattern>
* *</servlet-mapping> **/
//webServlet注解的效果等同于上面的注释
@WebServlet(name = "springBootServlet",urlPatterns = "/springBootServlet")
public class springBootServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("springBootServlet");
super.doGet(req, resp);
}
}
编写方法方式
1.编写servlet 不加注解
public class springBootServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("springBootServlet2");
super.doGet(req, resp);
}
}
2.在启动类中编写返回值为ServletRegistrationBean的方法 添加Bean注解
//方式二 基于方法配置servlet
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean =new ServletRegistrationBean(new springBootServlet2());
bean.addUrlMappings("/springBootServlet2");
return bean;
}
3.运行启动类 两者均运行测类
@SpringBootApplication
@ServletComponentScan//方式一 基于注解配置servlet加上此注解
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
springBoot整合Filter
注解方式
编写一个Filter
//方式一 基于注解配置Filter
//此处webFilter设置与webServlet设置同理
@WebFilter(filterName = "springBootFilter",urlPatterns = {"/*","*.do"})
public class springBootFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException{
System.out.println("进入springBootFilter过滤器");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("离开springBootFilter过滤器");
}
}
启动测试与servlet一样 只需在启动类上加上一个注解@ServletComponentScan
编写方法方式
1.与配置servlet同理 所编写的filter不加注解
2.在启动类中编写返回值为FilterRegistrationBean方法 添加Bean注解
//方式二 基于方法配置Filter
@Bean
public FilterRegistrationBean getFilterRegistration(){
FilterRegistrationBean filter =new FilterRegistrationBean(new springBootFilter2());
filter.addUrlPatterns("/*");
return filter;
}
3.启动测试
运行启动类 两者均运行测类
@SpringBootApplication
@ServletComponentScan//方式一 基于注解配置加上此注解
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
springBoot整合Listener
注解方式
编写一个Listener
//方式一 基于注解配置Listener
//此处的WebListener不需要进行设置
@WebListener
public class springBootListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("springBootListener启动了");
}
}
2.启动测试与servlet一样 只需在启动类上加上一个注解@ServletComponentScan
编写方法方式
1.与配置servlet同理 所编写的filter不加注解
2.在启动类中编写返回值为ServletListenerRegistrationBean方法 添加Bean注解
//方式二 基于方法配置Listener
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
ServletListenerRegistrationBean listener =new ServletListenerRegistrationBean(new springBootListener2());
return listener;
}
3.启动测试
运行启动类 两者均运行测类
@SpringBootApplication
@ServletComponentScan//方式一 基于注解配置加上此注解
public class Springboot01Application {
public static void main(String[] args) {
SpringApplication.run(Springboot01Application.class, args);
}
}
测试结果