Spring Boot integration Servlet (two ways)
- Create a new maven project
to create a structure diagram after completion:
- Pom.xml dependent on the introduction of
<!--引入父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<dependencies>
<!--SpringBoot web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
The first way (to complete the registration Servlet component by annotating scan mode):
- Complete Servlet component by way of annotations scanning registered
1.1. Creating a Servlet
1.2 Servlet write the code:
@WebServlet(name = "firstServlet",urlPatterns = "/firstServlet") //urlPatterns:访问路径
public class firstServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("进来了firstServlet");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
1.3 start writing class
to create springboot startup class
Code:
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet注解,并创建该类的实例
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Note: On startup class need to add @ServletComponentScan comment means: when you start scanning instance @WebServlet notes, create a Servlet
1.4 running start classes in your browser and enter localhost: 8080 firstServlet /
console output
The second way (by means of Servlet components to complete registration)
- Creating a Servlet
- Creating springboot classes start
the new method in the main components of a registered Servlet method
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
//添加一个方法,方法名无要求,必须返回ServletRegistrationBean。注册Servlet对象
@Bean //主键等价于<bean>标签
public ServletRegistrationBean<SecondServlet> getServletRegistrationBean(){
ServletRegistrationBean<SecondServlet> bean=
new ServletRegistrationBean<SecondServlet>(new SecondServlet(),"/SecondServlet");
return bean;
}
}
- Run startup class in your browser type localhost: 8080 / SecondServlet
- Print Console Information
Springboot Integration Filter (Servlet and integrated manner similar)
The first embodiment (to complete the registration Fliter annotation component by scanning)
- Create a Filter class
- Filter inherit parent class implements the interface
code is as follows:
@WebFilter(filterName = "firstFilter", urlPatterns = "/firstFilter")
public class firstFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("----进入FirstFilter-----");
chain.doFilter(request, response);//放行
System.out.println("----离开FirstFilter-----");
}
}
- Create a startup class
code is as follows:
@SpringBootApplication
//在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Running start classes in your browser and enter localhost: 8080 / firstFilter
here because the newspaper did not write path 404 after the release;
Print Console information:
The second way (to complete the registration Filter component by ways and means)
- Create a Filter class without writing notes @WebFilter
- Start the class
code is as follows:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
//添加一个方法
@Bean
public FilterRegistrationBean<SecondFilter> getFilterRegistrationBean(){
FilterRegistrationBean<SecondFilter> bean=
new FilterRegistrationBean<SecondFilter>(new SecondFilter());
bean.addUrlPatterns("*.do","*.jsp","/SecondFilter"); //以.do , .jsp , SecondFilter结尾路径的都会进到过滤器
return bean;
}
}
- Run startup class in your browser type localhost: 8080 / SecondFilter
console print information:
Springboot integration Listener (ibid)
Fliter complete assembly by scanning Register notes
- Create a class Listener
Listener Code:
@WebListener()
public class firstListener implements ServletContextListener{
//监听application对象的创建
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("-----------application对象创建-----------------");
}
}
- Create a startup class
Code:
@SpringBootApplication
@ServletComponentScan //在spring boot启动时会扫描@WebServlet @WebFilter @WebListener注解,并创建该类的实例
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
- Run startup class to see the console print info
The second way (by the method completes Listener component registration)
As will be omitted code directly on the code Code
- Create a Listener class
- Start Class
Code:
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Bean
public ServletListenerRegistrationBean<firstListener> getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<firstListener> bean=
new ServletListenerRegistrationBean<firstListener>(new firstListener());
return bean;
}
}
- Run startup class to see the console print info
Springboot access static resources (two ways)
The first method (static resources by looking at the root of the ServletContext)
1. Create in src / main directory of a webapp (directory name must be a webapp)
to create a different directory in the webapp to store different static resources, such as: images put pictures.
2. Run to start direct access to resources like access path
The second way (look for static resources from the directory classpath / static's)
Create a static directory under src / main / resources (directory name must be static)
create different directories stored in static different static resources, such as:. Images put pictures
2. Run to start the class browser to directly access resources access path
These are related to the contents of this tutorial, thanks for watching, please indicate the source