Understanding Domain Objects and Data Sharing in SpringMVC
Overview of Domain Objects
JavaWeb applications rely on four fundamental domain objects for data sharing across different scopes: PageContext, Request, Session, and Application (ServletContext). Each domain object functions as a Map implemantation, enabling data transfer between components within the same scope. These objects facilitate data access and modification throughout request processing lifecycle, supporting various web application interaction patterns.
Core Methods for All Domain Objects
// Store data in the domain
setAttribute(String name, Object value);
// Retrieve data from the domain
getAttribute(String name);
// Remove data from the domain
removeAttribute(String name);
Scope Hierarchy
| Domain Object | Scope Description |
|---|---|
| pageContext | Current JSP page only |
| request | Single request, shared across resources |
| session | Single session, multiple requests |
| servletContext | Entire applicasion, across all sessions |
The scope hierarchy follows this order: pageContext < request < session < servletContext
Lifecycle Details
- pageContext: Created when a JSP page is requested, destroyed after response
- request: Created when a request arrives, destroyed after response completes
- session: Created on first invocation (via request.getSession()), destroyed on server timeout (default 30 minutes), explicit invalidate() call, or abnormal server shutdown
- servletContext: Created when the application starts, destroyed when the application stops
SpringMVC provides multiple approaches for sharing data within the request scope, including ServletAPI, ModelAndView, Model, Map, and ModelMap interfaces.
Sharing Data to Request Scope
Using ServletAPI (HttpServletRequest)
@Controller
public class DataController {
@RequestMapping("/retrieveUserData")
public String retrieveUserData(HttpServletRequest httpRequest) {
httpRequest.setAttribute("userData", "Hello from ServletAPI");
return "result";
}
}
Access the data in the view (result.jsp):
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Result Page</title>
</head>
<body>
<h3>Data retrieved: ${userData}</h3>
</body>
</html>
Using ModelAndView
@RequestMapping("/fetchUserData")
public ModelAndView fetchUserData() {
ModelAndView viewModel = new ModelAndView();
// Add data to request scope
viewModel.addObject("userData", "Hello from ModelAndView");
// Set the view name for navigation
viewModel.setViewName("result");
return viewModel;
}
Using Model Interface
@RequestMapping("/loadUserData")
public String loadUserData(Model dataModel) {
dataModel.addAttribute("userData", "Hello from Model");
return "result";
}
Using Map
@RequestMapping("/obtainUserData")
public String obtainUserData(Map<String, Object> dataMap) {
dataMap.put("userData", "Hello from Map");
return "result";
}
Using ModelMap
@RequestMapping("/getUserData")
public String getUserData(ModelMap dataContainer) {
dataContainer.addAttribute("userData", "Hello from ModelMap");
return "result";
}
Sharing Data to Session Scope
@RequestMapping("/establishSession")
public String establishSession(HttpSession userSession) {
userSession.setAttribute("sessionData", "Hello from Session");
return "result";
}
Sharing Data to Application Scope
Access ServletContext through the session object:
@RequestMapping("/initializeApplication")
public String initializeApplication(HttpSession userSession) {
ServletContext appContext = userSession.getServletContext();
appContext.setAttribute("appData", "Hello from Application");
return "result";
}