springboot learning [version 2.6.2] error handling, Web native component injection day4-3

Posted by sharyn on Mon, 10 Jan 2022 17:27:38 +0100

SpringBoot error handling

By default, Spring Boot provides a / error mapping to handle all errors, and will be registered as a global error page in the servlet container
json will be generated on the machine client
The browser client will generate the whitelabel view
They all contain error messages

use

Error handling is so well packaged in springBoot that we can directly write 404500 html to automatically jump when there is an error in the browser
So you just

That's it

Web native component injection (distributed)

catalogue

Servlet

It is a server-side program written in Java for small service program or service connector, which is independent of platform and protocol
The main function is to interactively browse and generate data and generate dynamic Web content

MyServlet

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

@WebServlet("/my")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("this is my servlet");
    }
}

Note the @ WebServlet annotation

@WebServlet("/my")

This annotation is used to tell SpringBoot that it is a servlet written by itself and needs to access the path in ()

Day3Application

package com.example.day3;

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

@ServletComponentScan(basePackages = "com.example.day3")
@SpringBootApplication
public class Day3Application {

    public static void main(String[] args) {
        SpringApplication.run(Day3Application.class, args);
    }

}

Note the @ ServletComponentScan annotation

@ServletComponentScan(basePackages = "com.example.day3")

This annotation must be written. This annotation is used to scan the servlet under our project package. It cannot work without writing

Result display

Filter

Filter, also known as filter, is the most practical technology in Servlet Technology. Filter can intercept all web resources managed by web server, such as Jsp, Servlet, static picture file or static html file, so as to realize some special functions. For example, it implements some advanced functions such as URL level permission access control, filtering sensitive words, compressing response information and so on.
It is mainly used to preprocess user requests or post process HttpServletResponse.

MyFilter

package com.example.day3.servlet;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//Set to intercept all resources under the imgs package
@WebFilter(urlPatterns = {"/imgs/*"})
@Slf4j
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter Initialization complete");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("MyFilter implement");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        log.info("MyFilter Destroy");
    }
}

Note the @ WebFilter annotation

//Set to intercept all resources under the imgs package
@WebFilter(urlPatterns = {"/imgs/*"})

Listener

Listen for specific events in the web, such as the creation and destruction of ServletContext, httpsession and ServletRequest; Creation, destruction and modification of variables

MyListener

package com.example.day3.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
@Slf4j
public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
       log.info("MyListener Listening item initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MyListener Monitor item destruction");

    }
}

Note the @ WebListener annotation

@WebListener

Use display


Web native component injection (monolithic) uses RegistrationBean

servlet, filter and listener are written in the way of using RegistrationBean as a whole
We still need to write separate servlet, filter and listener classes ourselves
Compared with distributed, the difference is that there is no need to write @ webservlet, @ webfilter and @ weblistener annotations. Instead, they are created in the way of methods in the user-defined RegistrationBean configuration class

catalogue

MyServlet

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


public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("this is my servlet");
    }
}

MyFilter

package com.example.day3.servlet;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@Slf4j
public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter Initialization complete");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("MyFilter implement");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        log.info("MyFilter Destroy");
    }
}

MyListener

package com.example.day3.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@Slf4j
public class MyListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
       log.info("MyListener Listening item initialization");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        log.info("MyListener Monitor item destruction");

    }
}

(*)MyRegistrationConfig

Note that @ Bean is used for registration on each method

package com.example.day3.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
public class MyRegistrationConfig {
    @Bean
    public ServletRegistrationBean myServlet() {
        MyServlet myServlet = new MyServlet();
        return new ServletRegistrationBean(myServlet, "/my");
    }

    @Bean
    public FilterRegistrationBean myFilter() {
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        //Set interception path
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/my", "/img/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean myListener(){
        MyListener myListener = new MyListener();
        return new ServletListenerRegistrationBean(myListener);
    }
}

Topics: Java Front-end Spring Boot