A request scope </ font> is generated for each request. The saved instance will be available until a response is returned. This scope allows you to share an instance between the forward source and the forward destination.
Request scope operations are achieved by using the methods of the HttpServletRequest instance.
Save to request scope
request.setAttribute("Attribute name" <String>,instance<Object>);
Get an instance from the request scope
Acquired type name= (Acquisition type) request.getAttribute("Attribute name" <String>);
RequestScopeSample.java
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//Instance generation
Human human = new Human("Max", 25);
//Save instance in request scope
request.setAttribute("human", human);
//Get an instance from the request scope
Human h = (Human) request.getAttribute("human");
}
When using request scope in a JSP file, use the implicit object "request".
Use request scope in JSP file
<%@ page import="model.Human" %>
<%
//Get an instance from the request scope
Human h = (Human) request.getAttribute("human");
%>
<%= h.getName() %>You are<%= h.getAge() %>I'm old.
Recommended Posts