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{
}
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.
//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.
A scope that is retained unless the browser is closed or times out.
//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");
The scope that is retained until the application server is terminated. It is retained even if the browser is closed.
//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");
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.
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.
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")
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.
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