Web Program Object Scope:
There are three commonly used: request scope, Session scope, application context.
The request scope req The smallest, requires the least resources, functions the current request
Session sessions are used for this conversation, and each conversation is Jsessionid,
The ServletContext scope is large: all of the Web applications are accessible, the lifecycle is as long as the Web container, and more resources are required to maintain.
The less resources you need to meet your needs, the better.
One, request scope:
Get the properties and values of the page
Req.getparameter ("UserName");
enumeration<string> Enus = Req.getparameternames ();//Gets the parameter item submitted by the page
while (Enus.hasmoreelements ()) {
String propertyname = (string) enus.nextelement ();//Get Specific property name
String propertyvalue = Req.getparameter (propertyname);//Gets the value of the specific property
Params.put (PropertyName, PropertyValue);
}
Properties and values that can be set
Request.setsttribute ("Pricelist", pricelist);
List pricelist = (list) request.getattribute ("Pricelist");
Second, session scope
Req.getsession (). SetAttribute ("user", user), or//the object to be queried to the session, by the session to maintain the status of this person
Req.getsession (). Setmaxinactiveinterval (60);//sets the effective time, in seconds, of a special object within the session scope
Over this time, such as on the page to refer to this, there is no
<!--Global Settings session timeout length, in minutes--
<session-config>
<session-timeout>15</session-timeout>
</session-config>
Third, Application context
@WebServlet (urlpatterns= "/regist", asyncsupported=true,initparams={@WebInitParam (name= "password", value= "123456" )})
public class Registservlet extends HttpServlet {
private String password;
@Override
public void init (ServletConfig config) throws servletexception {
TODO auto-generated Method Stub
Password = config.getinitparameter ("password");
}
}
<!--Set default parameters to the entire application context (servlet run environment, similar to JVM), which is an initialization setting-
<context-param>
<param-name>qianduoduo</param-name>
<param-value>5000000</param-value>
</context-param>
String value = Getservletconfig (). Getservletcontext (). Getinitparameter ("Qianduoduo");
ServletContext sc = getservletconfig (). Getservletcontext ();
Sc.setattribute ("Qianshaoshao", 1);
Integer Qianshaoshao = (integer) getservletconfig (). Getservletcontext (). getattribute ("Qianshaoshao");
Web Program Object Scope