What is Session in Spring Boot and how to use it

What is Session in Spring Boot and how to use it

In Web applications, Session is a very common concept. It is used to maintain state information between the client and the server, such as login status, shopping cart contents, etc. In Spring Boot, Session is also an important concept. This article will introduce what Session in Spring Boot is and how to use it.

Insert image description here

What is Session

Session is a mechanism for maintaining state information between client and server. How it works is: when the client sends a request to the server, the server creates a Session for the request and assigns a unique identifier to the Session. This identifier will be sent to the client and stored in the client's cookie. When the client sends the next request, the client will send the identifier to the server again, and the server uses the identifier to obtain the corresponding Session and thus obtain the previously saved state information.

In Spring Boot, Session is implemented through the HttpSession interface. We can get and set properties in Session through the HttpSession interface, for example:

@Controller
public class MyController {
    
    
    @GetMapping("/login")
    public String login(HttpSession session) {
    
    
        session.setAttribute("username", "张三");
        return "login";
    }

    @GetMapping("/home")
    public String home(HttpSession session) {
    
    
        String username = (String) session.getAttribute("username");
        System.out.println("当前登录用户:" + username);
        return "home";
    }
}

In the above code, we define two request processing methods, namely /loginand /home. In /loginthe method, we set a property named through the HttpSession interface username. In /homethe method, we obtain the attribute through the HttpSession interface usernameand print out the username of the currently logged in user.

Use Session

Using Session in Spring Boot is very simple. Just follow the steps below to configure it.

1. Add dependencies

First you need to pom.xmladd the following dependencies to the file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

This dependency contains the core functionality of Spring Boot web applications and some commonly used dependencies.

2. Configure Session

In Spring Boot, we can application.propertiesconfigure some properties of Session through files. For example, we can set the Session timeout to 30 minutes through the following configuration:

server.servlet.session.timeout=30m

In the above configuration, we used server.servlet.session.timeoutthe attribute to set the Session timeout. The value of this attribute can be a time period, for example 30mrepresenting 30 minutes.

3. Use Session

When using Session, we can get and set the properties in Session through the HttpSession interface. For example, in the code above, we use session.setAttributethe method to set a usernameproperty named and session.getAttributethe method to get usernamethe property.

In Spring Boot, we can also get and set properties in Session through annotations. For example, we can use @SessionAttributesannotations to mark a controller class to indicate which properties in the Session the controller class needs to use. For example:

@Controller
@SessionAttributes("username")
public class MyController {
    
    
    @GetMapping("/login")
    public String login(Model model) {
    
    
        model.addAttribute("username", "张三");
        return "login";
    }

    @GetMapping("/home")
    public String home(@ModelAttribute("username") String username) {
    
    
        System.out.println("当前登录用户:" + username);
        return "home";
    }
}

In the above code, we use @SessionAttributesannotations to mark a controller class and specify the Session attribute name to be used username. In /loginthe method, we use Model.addAttributethe method to set usernamethe attribute. In /homethe method, we use @ModelAttributethe annotation to obtain usernamethe attribute.

Sample code

Below is a complete sample code that demonstrates how to use Session in Spring Boot.

Application.java

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

MyController.java

@Controller
@SessionAttributes("username")
public class MyController {
    
    
    @GetMapping("/login")
    public String login(Model model) {
    
    
        model.addAttribute("username", "张三");
        return "login";
    }

    @GetMapping("/home")
    public String home(@ModelAttribute("username") String username) {
    
    
        System.out.println("当前登录用户:" + username);
        return "home";
    }
}

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <p>当前登录用户:${username}</p>
    <a href="/home">进入主页</a>
</body>
</html>

home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
    <h1>Home</h1>
    <p>当前登录用户:${username}</p>
</body>
</html>

In the above sample code, we define a MyControllercontroller class named which contains two request processing methods /loginand /home. In /loginthe method, we use Model.addAttributethe method to set a usernameSession property named and return loginthe view. In loginthe view, we use ${username}an expression to get usernamethe attribute and display it on the page. In /homethe method, we use @ModelAttributeannotations to get usernamethe properties and print them to the console. In homethe view, we also use ${username}expressions to get usernamethe properties and display them on the page.

Summarize

Session is a mechanism for maintaining state information between the client and the server. It is also a very important concept in Spring Boot. Through the introduction of this article, we have learned about the basic concepts and usage of Session in Spring Boot, and how to simplify the code through annotations. I hope this article can help everyone better understand and use Session in Spring Boot.

Guess you like

Origin blog.csdn.net/JasonXu94/article/details/131609523