Servlet three scopes: Request, Session, Application

Request scope

When the request is forwarded from one action to a jsp time, if you want to use jsp action class variables, then we need to put into action the variables in the request scope to pass jsp. So you can get to the jsp the variable by request scope.

For example: After the successful landing personnel information needs to be displayed in the success page.
Request object is similar to a map collection. When put data into key-value pair, the key values through the data fetch.
request.setAttribute (key, value); // data to the request scope is placed inside.
request.getAttribute (key); // get the data from the inside requtest scope.
All scopes have two methods above.

Request scope features:
put data servlet and servlet must ensure that access to data using the same request object. Once the request object has changed, so, request a scope which values will fail.

session scope

session.setAttribute (key, value); // put data to the session scope
session.getAttribute (key); // get the data from the session scope
session.removeAttibute (key); // data is removed from the inside of the session scope.

Application Scope: ServletContext

ServletContext summary:
1.ServletContxt is to encapsulate web.xml file.

2. A project is only a web.xml file.

3.ServletContext objects in a project only one.

ServletContext Scope:
sc.setAttribute (Key, value); variables to the scope
sc.getAttribute (key); acquisition variable scope from the inside
sc.removeAttribute (key); remove the object from inside the scope.

Comparison of the three scopes

request, session, application (ServletContext)
minimum range Request scope, corresponding to the first request.
Session scope range should be larger. Corresponds to a session. As long as the browser is not related to a session.
** The maximum range Application scope. Corresponds to an application. ** No matter how many browsers, as long as access to the same project, then their position is a application object.

Application object associated with a project, he would have been created when tomcat starts, tomcat shut down when they were destroyed.

Guess you like

Origin blog.csdn.net/MacWx/article/details/92414302