Difference of 3 xxx.setAttribute()

Viewed 135

I'm New to SpringBoot,

I found 3 methods to send parameter from backend to frontend.

java code: https://paste.ubuntu.com/p/dtbBJVX3jQ/

HttpSession.setAttribute()

HttpServletRequest.getServletContext().setAttribute()

WebRequest.setAttribute()

Could you tell me what's the difference between 3 setAttribute() when in usage?

Thanks for your help

1 Answers
  • HttpSession is part of the Java EE API. It contains session scoped attributes. A session usually spans multiple requests, so whatever you save in a session attribute stays there until the session expires.
  • WebRequest is a Spring Web abstraction. It allows you to get and set attributes in different scopes (SCOPE_REQUEST, SCOPE_SESSION). A session-scoped attribute is the same as for HttpSession. A request-scoped attribute only lives for the duration of the request.
  • HttpServletRequest.getServletContext().setAttribute() is probably not what you need. ServletContext is created only once per web application, so you could only use it to save some global attributes that don't change for different requests.
Related