Java Servlet / JSP Request Scope Part 2

What is request scope?

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.

The identity of the request scope

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>);

Sample program

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");
}

Sample program --JSP file-

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

Java Servlet / JSP Request Scope Part 1
Java Servlet / JSP Request Scope Part 2
Java Servlet / JSP View drawing
[Java] Servlet filter
java practice part 1
Java variable scope (scope)
Java variable scope
Java session scope
What is the Java Servlet / JSP MVC model?
About [servlet] [JSP] [tomcat]
Java Servlet LifeCycle Example
Studying Java ~ Part 8 ~ Cast
Until you create a Web application with Servlet / JSP (Part 1)
Reasons to use Servlet and JSP separately in Java development
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
45 Java Performance Optimization Techniques (Part 1)
[Java] Beginner's understanding of Servlet-②
Java and Iterator Part 1 External Iterator
[Java] Beginner's understanding of Servlet-①
Apache Hadoop and Java 9 (Part 1)
Getting Started with JSP & Servlet
JSP error display from Servlet
Java Servlet 4.0 web.xml schema definition
Comparison of WEB application development with Rails and Java Servlet + JSP