How to use scope (JSP & Servlet)

To use the instance created by the Servlet class in the JSP file, use the area called Scope where the instance can be saved. There are three types of scopes to use: "request scope", "session scope", and "application scope", and the expiration date when the instance is saved differs depending on each scope.

Below is the basic syntax for each scope.

Request scope

Use when you want to share data between requests.

python


//Save to request scope
  request.setAttribute("Attribute name",instance);
//Get from request scope
Instance type Variable name= (Instance type) request.getAttribute("Attribute name");

Session scope

Used when you want to share data between HTTP sessions

python


//Get Session scope(Secure storage area) 
  HttpSession session = request.getSession(true);
//Save to session scope
  session.setAttribute("Attribute name",instance);
//Obtained from session scope
Instance type Variable name= (Instance type) session.getAttribute("Attribute name");
//Erase from session scope
  session. removeAttribute ("Attribute name");
//Discard session scope(End)
  session.invalidate();

Application scope

Used when you want to share data between web applications.

python


//Get application scope(Secure storage area)  
  ServletContext sc = getServletContext();
//Save to application scope
  sc.setAttribute("Attribute name",instance);
//Get from application scope
Instance type Variable name= (Instance type) sc.getAttribute("Attribute name");  
//Erase from application scope
  session.removeAttribute ("Attribute name");

The table below summarizes it. scope0.jpg

Recommended Posts

How to use scope (JSP & Servlet)
[Rails] How to use Scope
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use scope and pass processing (Jakarta)
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
How to use Lombok now
[Creating] How to use JUnit
How to use the link_to method
[Rails] How to use gem "devise"
How to use Lombok in Spring