您当前的位置: 首页 > 

哆啦A梦_i

暂无认证

  • 2浏览

    0关注

    629博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Web程序对象作用域:请求作用域,会话作用域,应用上下文

哆啦A梦_i 发布时间:2021-04-07 00:48:49 ,浏览量:2

Web程序对象作用域:

常用的有三个:请求作用域,会话作用域,应用上下文。

  • 请求作用域req范围最小,需要的资源最少,作用当前请求

  • session会话作用于本次对话,每个对话都有JSessionID

  • ServletContext作用域范围大:web应用中所有都能够访问,生命周期和web容器一样长,维护所需资源多

  • 在满足需求内耗费的资源越小越好

一、请求作用域:

//得到页面的属性和值
req.getParameter("userName");
Enumeration enus = req.getParameterNames();//获取页面所提交的参数项
while (enus.hasMoreElements()) {
String propertyName = (String) enus.nextElement();//得到具体的属性名称
String propertyValue = req.getParameter(propertyName);//得到具体的属性的值
params.put(propertyName, propertyValue);
}

//可以设置的属性和值
request.setSttribute("priceList",priceList);
List priceList = (List)request.getAttribute("priceList");

二、会话作用域

req.getSession().setAttribute("user", user);//将查询出的对象交给Session,由Session去维护这个人的状态
req.getSession().setMaxInactiveInterval(60);//设置Session作用域内的特殊对象的有效时间,以秒为单位
//超过这个时间,比如在页面要引用这个,就没有了




15

三、应用上下文

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



qianduoduo
5000000


String value = getServletConfig().getServletContext().getInitParameter("qianduoduo");


ServletContext sc = getServletConfig().getServletContext();
sc.setAttribute("qianshaoshao", 1);
Integer qianshaoshao = (Integer) getServletConfig().getServletContext().getAttribute("qianshaoshao");
关注
打赏
1556978864
查看更多评论
立即登录/注册

微信扫码登录

0.0583s