In this section, we are familiar with the three components of servlet, servlet, filter, listener, and the built-in Tomcat of SpringBoot.
Springboot does not support JSP (because of embedded Tomcat), so I will make SpringBoot support JSP, and the rest is the same.
It’s long, maybe in two sections.
1. The servlet configuration
Write a class that inherits HttpServlet configuration and then configure the mapping path of the servlet in the original web. XML file.
Now SpringBoot configures servlets, much the same way, to write a servlet, but remember that SpringBoot doesn’t recognize servlets directly, You need a tool, ServletRegistrationBean (xxxBean should be common in Spring, essentially a Bean, but with the ability to produce beans, like a simple factory class)
Let’s add the following code to a random configuration class (I added it to the main class for convenience) :
Ok, that’s it, and then run, browser type in the url
2. Configure the Filter
The configuration is similar to the above. Write a Filter implementation class and add a Filter to the container using the FilterRegistrationBean
Then run SpringBoot and type XXXXX /hehe
3. Configure the listener
The listener is the easiest, because the first two need to configure the URL mapping. Write a listener, use ServletListenerRegistrationBean added to the container.
Start and close the application to see the result:
The last is the class note ServletListenerRegistrationBean introduced into generics, to open the class, you can see we can customize the type of the listener
4, a brief introduction to the embedded Servlet container (springBoot default is of course tomcat)
The biggest difference between Springboot application and previous SSM applications is that Springboot can be made into jar packages, and then installed in the Java operating environment of the computer, you can use java-jar xxx.jar can directly run a Springboot application, very convenient, But the disadvantage is that JSP is not supported;
Then configure the Java environment on the new computer, download Tomcat, configure the version of Tomcat and Java, and then deploy the application and run, emmmm…. Probably a long time!
The biggest reason for this is that SpringBoot is loaded with a small Tomcat. You can EmbeddedServletContainer (…..) and you can EmbeddedServletContainer (tomcat). You can EmbeddedServletContainer (…..)
First, a brief description of servlet container types. Common Tomcat (springboot default), Jetty (for long connections, such as chat), and Undertow(no JSP support, but very good concurrency) all have their advantages.
Open the POM configuration file and you can see
Then change Tomcat to Jetty and Maven will automatically download the corresponding JAR packages for you
Run SpringBoot, the console output looks like this, and you’re done.
This section describes how to change the default configuration of the servlet container and how it works.
Port 8081 (default 8080) is the default port 8081 (default 8080). How to configure port 8081 (default 8080) in the configuration file. Can I change the container default parameters using @bean? And how does it work? Of course, we’ll walk you through the process of running the container from the main configuration class through a run method.
I will also talk about ways not to use the embedded servlet container. I used tomcat and Springboot applications that I downloaded from the outside and also support JSP.