Web Program Object Scope

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.