一、SpringBoot整合Servlet的两种方式
1.通过注解扫描完成Servlet组件的注册
编写Servlet
package com.example.demo.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*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 extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 8500147337364528371L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
System.out.println("FirstServlet...");
}
}
编写启动器类
package com.example.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication
@ServletComponentScan //在springBoot启动时会扫描@WebServlet,并将该类实例化
public class SpringbootWebApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootWebApplication.class, args);
} }
2.通过方法完成Servlet组件的注册
编写Servlet
package com.example.demo.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
*SpringBoot整合Servlet方式二
*
*/ public class SecondServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = -7076398985231616781L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("SecondServlet...");
} }
编写启动器类
package com.example.demo; 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.demo.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;
}
}
二、SpringBoot整合Filter的两种方式
1.通过注解扫描完成Filter组件的注册
编写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 class FirstFilter implements Filter { @Override
public void destroy() {
// TODO Auto-generated method stub }
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("进入Filter");
arg2.doFilter(arg0, arg1);
System.out.println("离开Filter");
} @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
编写启动类
/**
*SpringBoot整合Filter 方式一
*
*/
@SpringBootApplication
@ServletComponentScan
public class App3 { public static void main(String[] args) {
SpringApplication.run(App3.class, args); } }
2.通过方法完成Filter组件的注册
编写Filter
/**
*
*SpringBoot整合Filter 方式二
*
*/
public class SecondFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
System.out.println("进入SecondFilter");
arg2.doFilter(arg0, arg1);
System.out.println("离开SecondFilter");
} @Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
编写启动器类
/**
* SpringBoot整合Filter方式二
*
*
*/
@SpringBootApplication
public class App4 { public static void main(String[] args) {
SpringApplication.run(App4.class, args);
}/**
* 注册Filter
*/
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
//bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}
三、SpringBoot整合Listener的两种方式
1.通过注解扫描完成Listener的注册
编写Listener
/**
* springBoot整合Listener
*
*<listener>
* <listener-class>com.bjsxt.listener.FirstListener</listener-class>
*</listener>
*/
@WebListener
public class FirstListener implements ServletContextListener { @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub } @Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Listener...init......"); } }
编写启动器
/**
* springBoot整合Listener方式一
*
*
*/
@SpringBootApplication
@ServletComponentScan
public class App5 { public static void main(String[] args) {
SpringApplication.run(App5.class, args);
} }
2. 通过方法完成Listener组件注册
编写Listener
/**
* springBoot整合Listener方式二。
*
*
*/
public class SecondListener implements ServletContextListener { @Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
} @Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("SecondListener..init.....");
} }
编写启动器类
/**
* SpringBoot整合Listener方式二
*
*
*/
@SpringBootApplication
public class App6 { public static void main(String[] args) {
SpringApplication.run(App6.class, args); }
/**
* 注册listener
*/
@Bean
public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
return bean;
}
}
四、访问静态资源
1. SpringBoot可在classpath/static的目录下存放静态资源
2. SpringBoot可在ServletContext根目录下存放静态资源
在src/main/webapp 目录名称必须要webapp
五、文件上传
1.编写Controller类
/**
* SpringBoot文件上传
*
*
*/
//@Controller
@RestController //表示该类下的方法的返回值会自动做json格式的转换
public class FileUploadController { /*
* 处理文件上传
*/
@RequestMapping("/fileUploadController")
public Map<String, Object> fileUpload(MultipartFile filename)throws Exception{
System.out.println(filename.getOriginalFilename());
filename.transferTo(new File("d:/"+filename.getOriginalFilename()));
Map<String, Object> map = new HashMap<>();
map.put("msg", "ok");
return map;
}
}
2.编写启动器类
/**
* SpringBoot文件上传
*
*
*/
@SpringBootApplication
public class App7 { public static void main(String[] args) {
SpringApplication.run(App7.class, args); }
}
3.编写application.properties配置文件,设置上传文件大小的默认值
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
设置单个上传文件的大小:spring.servlet.multipart.max-file-size=200MB
设置一次请求上传文件的总容量:spring.servlet.multipart.max-request-size=200MB
总结
本篇简单介绍了SpringBoot整合Servlet、Filter、Listener、以及SpringBoot上传文件的方式,下一篇将介绍SpringBoot视图层技术。