[Spring] Spring and the Web (the last chapter)

Keywords: Spring

Spring learning is coming to an end~~~~~~~~~

👉 spring-web 👉 Complete the student registration function.


  1. New maven
  2. Modify pom.xml: dependencies other than spring and mybatis, servlet and jsp dependencies
  3. Create entity class Student, corresponding to Student2 table
  4. Create dao interface and mapper file
  5. Create mybatis master profile
  6. Create service and implementation classes
  7. Create a servlet, receive the requested parameters, and call the service object
  8. Create jsp and submit request parameters
  9. Create a jsp as a view to display the processing results of the request
  10. Create spring configuration file

1. Create a maven - web project

2. Add dependency

<!-- servlet rely on -->

    <!-- jsp rely on -->


    <!--Listener dependency-->

    <!--spring rely on-->

    <!--spring Transaction dependency-->

    <!--mybatis rely on-->

    <!--mybatis and spring integrate-->

    <!--mysql drive-->

    <!--Ali's connection pool-->

plug-in unit:


3. Directory structure: create java and resources and mark them

4. Create entity class and interface class

5. Create mapper file

6. Main configuration files: Spring, mybatis config, and properties

7. Create business layer service and implementation classes
Business level method: Verb + noun (see the meaning of name)

8. Create a servlet, receive the requested parameters, and call the service object

9. Create jsp and submit request parameters
Delete the original index.jsp and regenerate a JSP file with the idea template

Configure server:

There are records in the database

Query students:
form 👇

Create a new servlet:


In the doGet method of QueryStudentServlet:

Start the server and enter the address in the browser:

After looking for it for a long time, I don't know what went wrong...... The query results are not displayed, but there is output object information on the console.

1 now the problem of using container objects

1. Create container objects many times
2. Create container objects in multiple servlet s

2 what kind of container object is needed

1. There is only one container object. You can create it once
2. Container objects should be shared throughout the project. Multiple servlet s can use the same container object

To solve the problem, use the listener ServletContextListener (the two methods are executed initially and destroyed)
In the listener, the created container object should be placed in the scope of ServletContext in the web application.

3 ContextLoaderListener listener object

ContextLoaderListener is a listener object provided by the spring framework. The listener is used to:
1. Create container object: once
2. Put the container object into the scope of ServletContext.

Use steps:
1.pom.xml adds dependency on spring Web

<!--Listener dependency-->

2.web.xml declaration listener

Default listener: configuration file read when creating container object: / WEB-INF/applicationContext.xml
Profile path used by custom container:
Context param: it is called context parameter, and it provides parameters to listeners

Listener settings:

Start server: successful

4. Contextloaderlistener source code

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    //Initial method of listener
    public void contextInitialized(ServletContextEvent event) {


Initialization method internal: core part

View context:

private WebApplicationContext context;
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
            try {
                if (this.context == null) {
                    //Create container object for srping
                    this.context = this.createWebApplicationContext(servletContext);
           //Put the container object into the ServletContext scope of the
          //value = container object
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,                             this.context);
            } catch (Error | RuntimeException var8) {


WebApplicationContext is a container object used in a web project

public interface WebApplicationContext extends ApplicationContext 

Create container objects through the above method: no matter how many requests are made, only one object is created and shared.

It's much easier to remove the logs from the mybatis file

Request query multiple times:

//Use the tools and methods provided by Spring to obtain the container object
ServletContext sc = getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);


//Use the tools and methods provided by Spring to obtain the container object
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

Posted by FadeToLife on Thu, 11 Nov 2021 00:39:00 -0800