In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how SpringBoot integrates Servlet. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
SpringBoot integrates Servlet 1. Prepare the project:
To create a project using Spring Initializr, select the Web component
Second, method 1: through annotation method 1. Create a Servlet class
The @ WebServlet annotation needs to be added to the Servlet class, and the urlPatterns specifies the matching Url
/ / com.zhl.springbootweb.servlet.FirstServlet
Package com.zhl.springbootweb.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** integrates Servlet: * * / @ WebServlet (name = "FirstServlet", urlPatterns = "/ first") public class FirstServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println ("FirstServlet") }} 2. Modify startup class
Add @ ServletComponentScan annotation to the startup class
Package com.zhl.springbootweb;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;@SpringBootApplication/* scans @ WebServlet annotations when SpringBoot starts, and instantiates * / @ ServletComponentScanpublic class SpringbootWebApplication {public static void main (String [] args) {SpringApplication.run (SpringbootWebApplication.class, args);} third, method 2: injection via Bean. Create Servlet
No need to add @ WebServlet annotation
/ / com.zhl.springbootweb.servlet.SecondServlet
Package com.zhl.springbootweb.servlet;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/* integrates Servlet mode 2 * / public class SecondServlet extends HttpServlet {@ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println ("SecondServlet");}} 2. Create a configuration class
Use @ Configuration to identify as the configuration class
Use @ Bean to identify as a component
AddUrlMappings method to add URL match
/ / com.zhl.springbootweb.config.ServletConfig
Package com.zhl.springbootweb.config;import com.zhl.springbootweb.servlet.SecondServlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ServletConfig {/ * complete Servlet component registration * / @ Bean public ServletRegistrationBean getServletRegistrationBean () {ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean (new SecondServlet ()); servletRegistrationBean.addUrlMappings ("/ second"); return servletRegistrationBean }} after reading the above, do you have any further understanding of how SpringBoot integrates Servlet? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.