一,整合Servlet
1,通过注解扫描完成Servlet组件的注册
1.1编写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(name="FirstServlet",urlPatterns="/first")
public class FirstServlet extendsHttpServlet {
@Override
protected voiddoGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
// TODOAuto-generated method stub
super.doGet(req, resp);
}
}
1.2编写启动类
这里启动类就一个普通的启动类
/**
* SpringBoot整合Servlet方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public classApp {
public static voidmain(String[] args) {
SpringApplication.run(App.class, args);
}
}
2,通过方法完成Servlet组件的注册
2.1编写servlet
这里Servlet没有额外的标签
/**
*SpringBoot整合Servlet方式二
*
*/
public class SecondServlet extendsHttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
System.out.println("SecondServlet..........");
}
}
2.2编写启动类
通过@Bean,增加Servlet的Bean的注册
/** * SpringBoot整合Servlet方式二 * * */ @SpringBootApplication public classApp2 { public static voidmain(String[] args) { SpringApplication.run(App2.class, args); } @Bean publicServletRegistrationBean getServletRegistrationBean(){ ServletRegistrationBean bean= newServletRegistrationBean(newSecondServlet()); bean.addUrlMappings("/second"); return bean; } } |
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.bjsxt.servlet.SecondServlet;
/**
* SpringBoot整合Servlet方式二
*
*
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
bean.addUrlMappings("/second");
return bean;
}
}
二,整合Filter
1,通过注解扫描完成Filter组件的注册
1.1编写Filter
/**
*SpringBoot整合Filter 方式一
*<filter>
*<filter-name>FirstFilter</filter-name>
*<filter-class>com.bjsxt.filter.FirstFilter</filter-class>
*</filter>
*<filter-mapping>
*<filter-name>FirstFilter</filter-name>
*<url-pattern>/first</url-pattern>
*</filter-mapping>
*/
//@WebFilter(filterName="FirstFilter",urlPatterns={"*.do","*.jsp"})
@WebFilter(filterName="FirstFilter",urlPatterns="/first")
public classFirstFilter implementsFilter {
@Override
public voiddestroy() {
// TODOAuto-generated method stub
}
@Override
public voiddoFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throwsIOException, ServletException {
System.out.println("进入Filter");
arg2.doFilter(arg0, arg1);
System.out.println("离开Filter");
}
@Override
public voidinit(FilterConfig arg0) throwsServletException {
// TODOAuto-generated method stub
}
}
1.2编写启动类
/**
*SpringBoot整合Filter 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public classApp {
public static voidmain(String[] args) {
SpringApplication.run(App.class, args);
}
}
2,通过方法完成Filter组件的注册
2.1编写Filter
/**
*
*SpringBoot整合Filter 方式二
*
*/
public classSecondFilter implementsFilter {
@Override
public voiddestroy() {
// TODOAuto-generated method stub
}
@Override
public voiddoFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throwsIOException, ServletException {
System.out.println("进入SecondFilter");
arg2.doFilter(arg0, arg1);
System.out.println("离开SecondFilter");
}
@Override
public voidinit(FilterConfig arg0) throwsServletException {
// TODOAuto-generated method stub
}
}
2.2编写启动类
/**
* SpringBoot整合Filter方式二
*
*
*/
@SpringBootApplication
public classApp2 {
public static voidmain(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册Servlet
* @return
*/
@Bean
publicServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean= newServletRegistrationBean(newSecondServlet());
bean.addUrlMappings("/second");
return bean;
}
/**
* 注册Filter
*/
@Bean
publicFilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean= newFilterRegistrationBean(newSecondFilter());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}
三,整合Listener
1,通过注解扫描完成Listener组件的注册
1.1编写Listener
/**
* springBoot整合Listener
*
*<listener>
*<listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public classFirstListener implementsServletContextListener {
@Override
public voidcontextDestroyed(ServletContextEvent arg0) {
// TODOAuto-generated method stub
}
@Override
public voidcontextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......");
}
}
1.2编写启动类
/**
* springBoot整合Listener方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan
public classApp {
public static voidmain(String[] args) {
SpringApplication.run(App.class, args);
}
}
2. 通过方法完成Listener组件注册
2.1编写Listener
/**
* springBoot整合Listener方式二。
*
*
*/
public classSecondListener implementsServletContextListener {
@Override
public voidcontextDestroyed(ServletContextEvent arg0) {
// TODOAuto-generated method stub
}
@Override
public voidcontextInitialized(ServletContextEvent arg0) {
System.out.println("SecondListener..init.....");
}
}
2.2编写启动类
/**
* SpringBoot整合Listener方式二
*
*
*/
@SpringBootApplication
public classApp2 {
public static voidmain(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册listener
*/
@Bean
publicServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean= newServletListenerRegistrationBean<SecondListener>(newSecondListener());
return bean;
}
}
四,访问静态资源
1. SpringBoot从classpath/static的目录
注意目录名称必须是static
标签:Web,J2EE,SpringBoot,Spring,args,Filter,bean,Override,public From: https://blog.51cto.com/u_15590807/7727464