[JAVA] How to use scope and pass processing (Jakarta)

scope

A scope is a storage area used when you want to share data across Servlets and JSPs. There are the following three types depending on the sharing range.

--Request scope --Session scope --Application scope

Save to scope in the doGet or doPost methods of the Servlet There are many ways to pass it next with the forward method.

The template of the declaration part of doGet method and doPost method is as follows.

doGet,declaration of doPost method


// doGet
Public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException{
}

//same for doPost
Public void doPost(HttpServletRequest request, HttpServletResponse response)
      Throws ServletException, IOException{
}

Request scope

A scope generated for each request from the browser. In principle, it is not retained when the next request comes, the browser is closed, or the application server is restarted.

How to use

//Save to request scope
request.setAttribute("Attribute name",Instance you want to save);

//Get an instance from the request scope
Type name= (Mold) request.getAttribute("Attribute name");


//The attribute name is an index that points to the scope.
//A type is the type of a saved instance
//The name is the reference variable you want to give to the acquired instance.

Session scope

A scope that is retained unless the browser is closed or times out.

How to use

//Secure session scope storage area
HttpSession session = request.getSession();

//Save to session scope
Session.setAttribute("Attribute name",Instance you want to save)

//Get an instance from session scope
Type name= (Mold) session.getAttribute("Attribute name");

Application scope

The scope that is retained until the application server is terminated. It is retained even if the browser is closed.

How to use

//Secure application scope storage area
ServletContext sc = getServletContext();

//Save to application scope
sc.setAttribute("Attribute name",Instance you want to save)

//Get an instance from application scope
Type name= (Mold) sc.getAttribute("Attribute name");

Delivery of processing

When passing the process to the next Servlet or JSP, a method called forward is used to take over the data saved in the scope. When data is not inherited, processing can be passed by using redirect, but it is often used when passing it to an external site. The forward method can only pass the process within the server.

forward

A method of inheriting the data saved in the scope and passing the process to the next page. The server side automatically executes the following processing and returns it to the browser. When handing over processing within the same server, forward may be used even if there is no particular data handing over.

How to use

RequestDispatcher dispatcher = request.getRequestDispatcher("Forward destination");
dispatcher.forward(request, response);

//Requires the following packages
import javax.servlet.RequestDispatcher;

//For the forward destination, write the path according to the directory structure
//For example:
request.getRequestDispatcher("/WEB-INF/jsp/hogehoge.jsp")

redirect

The server returns to the browser where to access next, and the browser requests the next process from the specified destination. Due to this mechanism, it is suitable for accessing external sites.

How to use

request.sendRedirect("Redirect destination");

//The redirect destination may be specified by URL, or the path according to the file structure may be used within the same server.
//For example:
request.sendRedirect("http://www.hogehoge.com")
request.sendRedirect("/Servlet name")

Recommended Posts

How to use scope and pass processing (Jakarta)
[Rails] How to use Scope
How to use StringBurrer and Arrays.toString.
How to use scope (JSP & Servlet)
How to use EventBus3 and ThreadMode
How to use equality and equality (how to use equals)
[Processing × Java] How to use variables
[Processing × Java] How to use arrays
How to use OrientJS and OrientDB together
[Processing × Java] How to use the loop
How to set up and use kapt
[Processing × Java] How to use the class
[Processing × Java] How to use the function
How to use substring and substr methods
How to use @Builder and @NoArgsConstructor together
[Java] How to use FileReader class and BufferedReader class
How to use Map
How to use rbenv
How to use letter_opener_web
How to use fields_for
How to use java.util.logging
How to use map
[Ruby] How to use gsub method and sub method
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use Segmented Control and points to note
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
[Java] Note how to use RecyclerView and implementation of animated swipe processing.
How to use JUnit 5
How to execute processing before and after docker-entrypoint.sh
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
Rails scope anti-patterns and how to eliminate them
How to use Map
[Java] How to use Calendar class and Date class
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use and apply Java's JFrame / Canvas class
[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 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 ()