1024programmer Blog SpringBoot Learning 2: Integrating Web Technology_addurlmappings_kenewstar’s Blog

SpringBoot Learning 2: Integrating Web Technology_addurlmappings_kenewstar’s Blog

class=”markdown_views prism-atom-one-light”>

(1) SpringBoot integrates Servlet technology

Many beginners who are new to JavaEE will learn Jsp/Servlet in Web technology, and the same SpringBoot also provides integrated Servlet technology;

Create a Java project (IDEA+Maven)

Insert picture description here
Insert picture description hereInsert picture description here
The project is created;

There are two ways for SpringBoot to integrate Servlet:

1 Complete the registration of Servlet components by annotation
① Create a FirstServlet class to inherit HttpServlet

Create the code as follows:

package com.kenewstar.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;

 /**
  * @Author:kenewstar
  * @Description: For springboot integration servlet,
  * Method 1: Complete the registration of Servlet components through annotation scanning
  * @Date:Created in 2020/4/20
  */
 @WebServlet("/first") //url access path
 public class FirstServlet extends HttpServlet {
     

     @Override
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
         //Console print data
         System.out.println("springboot integrated servlet: use annotation method");
     }
 }

 

The above code does not explain too much, it is just a very common Servlet;
Next we need to create a SpringBoot startup class;

② Create SpringBoot startup class
package com.kenewstar;

 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.web.servlet.ServletComponentScan;  

 /**
  * @Author:kenewstar
  * @Description: spingboot startup class, test FirstServlet
  * @Date:Created in 2020/4/20
  */
 @SpringBootApplication
 @ServletComponentScan //used to scan @WebServlet annotation
 public class FirstApp {
     

     public static void main(String[] args  ) {
     
         //Start springboot
         SpringApplication.run(FirstApp.class,args);
     }
 }
 

The SpringBoot startup class has been written in the previous code, but it is just one more annotation, @ServletComponentScan, its function is to scan the Servlet class annotated with @WebServlet, let SpringBoot know that this class is a Servlet Class, when the SpringBoot startup class is started, the Servlet registration is completed, and we can access our project through the URL address mapping provided by the @WebServlet annotation;
After the project starts, enter the address:
Insert picture description here

The project visit results are as follows:
Insert picture description here
Printed in the console A piece of data;

2 Complete the registration of Servlet components by method

In the above code, we use annotations, but we can also complete the registration of Servlet components without using annotations;

① Create SecondServlet to inherit HttpServlet

The code of SecondServlet is as follows:

package com.kenewstar.servlet;

 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 /**
  * @Author:kenewstar
  * @Description: For springboot integration servlet
  * Method 2: Use the method to complete the registration of the Servlet component
  * @Date:Created in 2020/4/20
  */
 public class SecondServlet extends HttpServlet {
     

     @Override
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     

         System.out.println("springboot integration servlet: registration by using method");
     }
 }

 

In the above code, we can see that it is only one annotation different from the method in ①;
Next, we create the SpringBoot startup class;

② Create SpringBoot startup class

The code looks like this:

package com.kenewstar;

 import com.kenewstar.servlet.SecondServlet;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;;
 import org.springframework.boot.web.servlet.ServletRegistrationBean;  
 import org.springframework.context.annotation.Bean;

 /**
  * @Author:kenewstar
  * @Description: springboot startup class, used to test the second way of springboot integration Servlet
  * @Date:Created in 2020/4/20
  */
 @SpringBootApplication
 public class SecondApp {
     

     public static void main(String[] args  ) {
     
         //Start springboot
         SpringApplication.run(SecondApp.class,args);

     }
     @Bean
     public ServletRegistrationBean getServlet(){
     
         //Instantiate ServletRegistrationBean
         ServletRegistrationBean srb = new ServletRegistrationBean(new SecondServlet(  )).kenewstar.listener;

 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;

 /**
  * @Author:kenewstar
  * @Description: SpringBoot integrates Listener, the second way
  * @Date:Created in 2020/4/20
  */
 public class SecondListener implements ServletContextListener {
     

     @Override
     public void contextInitialized(ServletContextEvent sce) {
     
         System.out.println("Listener is initialized")  ;
     }
 }

 
② Create SpringBoot startup class

The code is as follows:

package com.kenewstar;
 import com.kenewstar.listener.SecondListener;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;  
 import org.springframework.context.annotation.Bean;

 /**
  * @Author:kenewstar
  * @Description: springboot startup class, used to test the second way of springboot integration Listener
  * @Date:Created in 2020/4/20
  */
 @SpringBootApplication
 public class SecondApp {
     

     public static void main(String[] args  ) {
     
         //Start springboot
         SpringApplication.run(SecondApp.class,args);

     }
     @Bean
     public ServletListenerRegistrationBeanSecondListener>   getListener()  {
     
         //Instantiate SerServletListenerRegistrationBean
         ServletListenerRegistrationBeanSecondListener slrb = new
                 ServletListenerRegistrationBean(new SecondListener());
         return slrb;

     }

 }

 

After starting the SpringBoot project, the console prints as follows:
Insert picture description here
The above is SpringBoot integrated Listener;

(4) SpringBoot accesses static resources

How do users access the static resources of the project, such as html, css, js, image files, etc.;

The first way: by accessing classpath/static

First, let’s look at the directory structure of the project:
Insert picture description here
Our Static resources are placed in the resources directory, but a directory named static must be created in this directory. All our static resources are placed in this directory, and the directory name must be static, that is, SpringBoot is from the classpath/static directory
insert picture description here
Put it under the static directory as shown above A pic1.png picture
Start the project and visit this resource:
Insert picture description here
In development, we generally place pictures in a folder, so we create an images directory under the static directory to store pic2.png, as follows:
Insert picture description here
So, when we visit the picture again, we need to bring the path:
Insert picture description here

The second way: create a webapp directory

In the ServletContext root directory, as shown in the figure below:
The directory name must be webapp, as follows we place a picture pic3.png in the directory
Insert picture description here
Access result:

Insert picture description hereWe are done here;

an> getListener(){

//Instantiate SerServletListenerRegistrationBean
ServletListenerRegistrationBeanSecondListener slrb = new
ServletListenerRegistrationBean(new SecondListener());
return slrb;

}

}

After starting the SpringBoot project, the console prints as follows:
Insert picture description here
The above is SpringBoot integrated Listener;

(4) SpringBoot accesses static resources

How do users access the static resources of the project, such as html, css, js, image files, etc.;

The first way: by accessing classpath/static

First, let’s look at the directory structure of the project:
Insert picture description here
Our Static resources are placed in the resources directory, but a directory named static must be created in this directory. All our static resources are placed in this directory, and the directory name must be static, that is, SpringBoot is from the classpath/static directory
insert picture description here
Put it under the static directory as shown above A pic1.png picture
Start the project and visit this resource:
Insert picture description here
In development, we generally place pictures in a folder, so we create an images directory under the static directory to store pic2.png, as follows:
Insert picture description here
So, when we visit the picture again, we need to bring the path:
Insert picture description here

The second way: create a webapp directory

In the ServletContext root directory, as shown in the figure below:
The directory name must be webapp, as follows we place a picture pic3.png in the directory
Insert picture description here
Access result:

Insert picture description hereWe are done here;

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/springboot-learning-2-integrating-web-technology_addurlmappings_kenewstars-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索