Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding Domain Objects and Data Sharing in SpringMVC

Tech 1

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";
}

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.