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.
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 /login
and /home
. In /login
the method, we set a property named through the HttpSession interface username
. In /home
the method, we obtain the attribute through the HttpSession interface username
and 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.xml
add 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.properties
configure 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.timeout
the attribute to set the Session timeout. The value of this attribute can be a time period, for example 30m
representing 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.setAttribute
the method to set a username
property named and session.getAttribute
the method to get username
the property.
In Spring Boot, we can also get and set properties in Session through annotations. For example, we can use @SessionAttributes
annotations 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 @SessionAttributes
annotations to mark a controller class and specify the Session attribute name to be used username
. In /login
the method, we use Model.addAttribute
the method to set username
the attribute. In /home
the method, we use @ModelAttribute
the annotation to obtain username
the 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 MyController
controller class named which contains two request processing methods /login
and /home
. In /login
the method, we use Model.addAttribute
the method to set a username
Session property named and return login
the view. In login
the view, we use ${username}
an expression to get username
the attribute and display it on the page. In /home
the method, we use @ModelAttribute
annotations to get username
the properties and print them to the console. In home
the view, we also use ${username}
expressions to get username
the 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.